我是相对较新的VBA。
我正在尝试使用此代码从网站获取一些信息。当我按元素执行时,我必须搜索标记名称tr
并使用旁边的数字来定义我想要使用的标记。问题在于它经常随着网站上的位置而变化。目前我想要搜索的关键字及其包含的信息如下:
<tr>
<td class="nt">Operations</td>
<td>Windows</td>
</tr>
因此,如果我可以通过课程&#34;操作&#34;进行搜索,并获取信息&#34; Windows&#34;,那将有所帮助。另外,我目前有错误
接下来没有For
如果可能的话,我可以在关闭页面之前使用它来进行多次搜索吗?所以我寻找多个特定的单词并将这些数据输入到不同的单元格中,然后移动到下一列,它将重复,直到在x值的末尾完成。我目前只将它设置为x = 2到5,但我希望将来将其增加到10或更高。
目前的代码如下所示。
Private Sub Worksheet_Change(ByVal Target As Range)
For x = 2 To 5
If Target.Row = Cells(x, 35).Row And _
Target.Column = Cells(x, 35).Column Then
'If Target.Row = Range("ManufacturerPartNumber").Row And _
'Target.Column = Range("ManufacturerPartNumber").Column Then
Dim IE As New InternetExplorer
'IE.Visible = True
'For x = 2 To 5
'IE.navigate "" & Range("Website_1").Value
IE.navigate "" & Cells(x, 35).Value
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.document
Dim sDD As String
sDD = InStr(1, IE.document.body.innerHTML, "Processor Model")
'sDD = Trim(Doc.getElementsByTagName("Processor Model")(1).innerText) 'Use this with tag like dd and number for which it appears like 0 or 1
IE.Quit
Dim aDD As Variant
aDD = Split(sDD, ",")
Cells(x, 44).Value = aDD(0)
'Range("ProcessorNumberCd").Value = aDD(0)
'Range("OSProvided").Value = aDD(0)
Next x
End If
'MsgBox "Complete"
End Sub
答案 0 :(得分:0)
我想你想抓住'内部文本'。看一下下面的例子。
Sub Scraper()
Dim item As Long
Dim priceStr As String
Dim priceTag As Object
Dim priceTable As Object
item = "10011" 'this will eventually be placed in a loop for multiple searches
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
' navigate and download the web page
objIE.Navigate "www.google.com"
Do While objIE.ReadyState <> 4 Or objIE.Busy
DoEvents
Loop
'objIE.Document.getElementsByTagName("input")(0).Value = item
'objIE.Document.getElementByID("FDI").Click
Set priceTable = objIE.Document.getElementByID("price_FGC")
Set priceTag = priceTable.getElementsByTagName("u")(3)
priceStr = priceTag.innerText
Sheet1.Range("A1").Value = priceStr
objIE.Quit
End Sub
另外,请查看此链接,了解如何执行其他类似操作的其他几种方式。
http://www.tushar-mehta.com/publish_train/xl_vba_cases/vba_web_pages_services/index.htm