2012-03-18 68 views
1

我有一个文件,我想从中提取日期,它是一个HTML源文件,因此它充满了我不需要的代码和短语。我需要提取多数民众赞成包裹在一个特定的HTML标签的日期的每个实例:从HTML标记中的文件中刮掉文本

缩写标题=“((这是我所需要的文本))”数据UTIME =”

什么是最简单的方法实现这一目标?

+0

这是*不*数据挖掘。重新命名并重新标记为[标签:网页抓取]。数据挖掘本质上是一种大规模*统计分析*;这至多是数据挖掘的预处理。哦,为什么这个标签为“excel”? – 2012-03-18 15:08:26

回答

0

,如果你使用的是Java,你可以使用Jsoup,这是从你的问题不清楚,请正是你想如果你在使用Excel VBA做

+0

所以基本上我有一个15MB的文件,这是从保存的网页提取。该页面包含Facebook消息,每条消息都标有日期,我想对每个日期的消息进行分析,因此每次出现日期时都需要提取。希望清除它。 – user1023420 2012-03-18 12:12:39

+0

如果编程语言并不重要,那么我肯定会推荐JSoup – 2012-03-18 12:47:35

+0

或BeautifulSoup,如果你更喜欢python。 – 2012-03-18 15:09:39

5

阐述,设置参考(工具 - 参考)到MSHTML库(在参考菜单中标为Microsoft HTML Object Library

Sub ScrapeDateAbbr() 

    Dim hDoc As MSHTML.HTMLDocument 
    Dim hElem As MSHTML.HTMLGenericElement 
    Dim sFile As String, lFile As Long 
    Dim sHtml As String 

    'read in the file 
    lFile = FreeFile 
    sFile = "C:/Users/dick/Documents/My Dropbox/Excel/Testabbr.html" 
    Open sFile For Input As lFile 
    sHtml = Input$(LOF(lFile), lFile) 

    'put into an htmldocument object 
    Set hDoc = New MSHTML.HTMLDocument 
    hDoc.body.innerHTML = sHtml 

    'loop through abbr tags 
    For Each hElem In hDoc.getElementsByTagName("abbr") 
     'only those that have a data-utime attribute 
     If Len(hElem.getAttribute("data-utime")) > 0 Then 
      'get the title attribute 
      Debug.Print hElem.getAttribute("title") 
     End If 
    Next hElem 

End Sub 

我认为文件是本地的,因为您在源文件中调用。如果你需要先下载它,你需要另一个参考MSXML和这个代码

Sub ScrapeDateAbbrDownload() 

    Dim xHttp As MSXML2.XMLHTTP 
    Dim hDoc As MSHTML.HTMLDocument 
    Dim hElem As MSHTML.HTMLGenericElement 

    Set xHttp = New MSXML2.XMLHTTP 
    xHttp.Open "GET", "file:///C:/Users/dick/Documents/My%20Dropbox/Excel/Testabbr.html" 
    xHttp.send 

    Do 
     DoEvents 
    Loop Until xHttp.readyState = 4 

    'put into an htmldocument object 
    Set hDoc = New MSHTML.HTMLDocument 
    hDoc.body.innerHTML = xHttp.responseText 

    'loop through abbr tags 
    For Each hElem In hDoc.getElementsByTagName("abbr") 
     'only those that have a data-utime attribute 
     If Len(hElem.getAttribute("data-utime")) > 0 Then 
      'get the title attribute 
      Debug.Print hElem.getAttribute("title") 
     End If 
    Next hElem 

End Sub