使用查询获取请求在VBA中解析HTML

时间:2018-10-24 12:54:16

标签: html excel vba web-scraping

我正在使用其他人的代码,因为这是其他人正在使用的旧文件,我想对其进行更新以提高效率,但是我需要一些帮助。下面是vba操作。我需要的是获取信息,但删除除某个单词以外的所有内容,该单词每次运行都会更改。我可以使用regex和objRE.Pattern =“ |”但是单词会根据状态而变化。

HTML:

<span  onmouseover="ShowText('Message','blahblah'); return true;" 
onmouseout="HideText('Message'); return true;" 
href="javascript:ShowText('Message')">---(PSA)---</span>
</font><a href='?srn=numbers12131131'target='_self'><font color='#6666FF' 
size='3'>numbers123232343</font></a><font  size='3'>----Installed----MUM 
Indication:In Scope-<font color='#00CC00'>PASS WITH WARNING</font>--- (20181018) 
</td><tr></table> </b><br>
<table class="OrderForm" width="1000"> '

我只想要Excel工作表中的“已安装”状态。

需要工作的VBA代码:

Sub GetComment()

Dim book As Workbook
Dim sheet As Worksheet
Dim row As Integer
Dim SRN As String
Dim whttp As Object


Set book = ThisWorkbook
Set sheet = book.Worksheets("CMT Data")
Set whttp = CreateObject("WinHTTP.WinHTTPrequest.5.1")


row = 2
SRN = sheet.Cells(row, 1)
Do While SRN <> ""
    Debug.Print SRN
    whttp.Open "GET", "www.websitedatgoeshere.com" & SRN, False
    whttp.SetRequestHeader "Cookie", "mycookiefromwebsite;"
    whttp.send
    Debug.Print whttp.responseText
    sheet.Cells(row, 2) = whttp.responseText
    row = row + 1
    SRN = sheet.Cells(row, 1)

Loop

Set whttp = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

这是基于且仅当单词始终在“ ----”和“ ----”之间,并且该单词是响应中的第一个出现时。如果不是第一个,则可以根据需要调整索引1。

Debug.Print Split(Split(whttp.responseText, "----")(1), "----")(0)
sheet.Cells(row, 2) = Split(Split(whttp.responseText, "----")(1), "----")(0)
相关问题