2013-05-18 5 views
4

텍스트를 추출해야하는 많은 HTML 파일이 있습니다. 모든 것이 한 줄에 있다면 아주 쉽게 할 수 있습니다. 그러나 태그가 여러 줄로 묶이거나 여러 줄로되어 있다면이 작업을 수행하는 방법을 생각할 수 없습니다. 이 주위의 텍스트 줄 바꿈 도움이 될 것입니다하지 않는HTML 태그 사이에서 텍스트 추출

<section id="MySection"> 
Some text here 
another line here <br> 
last line of text. 
</section> 

내가의 <br> 텍스트에 대해 우려하지 않다 : 여기가 무슨 뜻입니다. 내가 원하는 영역은 항상 "MySection"으로 시작하여 </section>으로 끝납니다. 내가 끝낼하고 싶은 것은이 같은 것입니다 : 나는 VBScript를 또는 명령 줄 옵션 같은 것을 원합니다

Some text here another line here last line of text. 

하지만 난 시작할 위치를 모르겠어요 (나오지?). 어떤 도움이 필요합니까? 여기

답변

4

일반적으로 인터넷 익스플로러를 사용합니다. 이것에 대한 연구의 COM 개체 : 그러나

root = "C:\base\dir" 

Set ie = CreateObject("InternetExplorer.Application") 

For Each f In fso.GetFolder(root).Files 
    ie.Navigate "file:///" & f.Path 
    While ie.Busy : WScript.Sleep 100 : Wend 

    text = ie.document.getElementById("MySection").innerText 

    WScript.Echo Replace(text, vbNewLine, "") 
Next 

<section> 태그는 getElementById("MySection")는 여는 태그를 반환으로 COM 객체가 제대로 처리하지 않는 것 이전에 IE 9, 심지어 IE 9에서 지원되지 않습니다 :

>>> wsh.echo ie.document.getelementbyid("MySection").outerhtml 
<SECTION id=MySection> 

당신은하지만, 대신에 정규 표현식을 사용할 수

root = "C:\base\dir" 

Set fso = CreateObject("Scripting.FileSystemObject") 

Set re1 = New RegExp 
re1.Pattern = "<section id=""MySection"">([\s\S]*?)</section>" 
re1.Global = False 
re2.IgnoreCase = True 

Set re2 = New RegExp 
re2.Pattern = "(<br>|\s)+" 
re2.Global = True 
re2.IgnoreCase = True 

For Each f In fso.GetFolder(root).Files 
    html = fso.OpenTextFile(filename).ReadAll 

    Set m = re1.Execute(html) 
    If m.Count > 0 Then 
    text = Trim(re2.Replace(m.SubMatches(0).Value, " ")) 
    End If 

    WScript.Echo text 
Next 
1

perl를 사용하여 한 줄 용액 Mojolicious 틀로부터 HTML 파서 :

perl -MMojo::DOM -E ' 
    say Mojo::DOM->new(do { undef $/; <> })->at(q|#MySection|)->text 
' index.html 

은 다음과 같은 내용으로 index.html 가정은 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
</head> 
<body id="portada"> 
<section id="MySection"> 
Some text here 
another line here <br> 
last line of text. 
</section> 
</body> 
</html> 

그것은 산출 :

Some text here another line here last line of text. 
+0

+1을 적절한 파서와 전체 우아한 솔루션을 사용하여 제안합니다. –

관련 문제