VBA和IE8 - 刮写在表格中的文字

时间:2016-09-05 14:50:32

标签: vba excel-vba web-scraping internet-explorer-8 excel

美丽的蒂姆·威廉姆斯,{p> I had an earlier question solved,但我又被卡住了。找到使用ie8进行抓取的资源非常困难。

这是我现在的代码

Sub Scraper()

Dim item As Long
Dim price1 As String
Dim obj 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 "http://*********.aspx?UID=" & item & "~***"
Do While objIE.ReadyState <> 4 Or objIE.Busy
    DoEvents
Loop

    'keeping lines for reference, but increased speed by adding data to URL - do not need anymore
    'objIE.Document.getElementsByTagName("input")(0).Value = item
    'objIE.Document.getElementByID("FGC").Click

Set housePrice = objIE.Document.getElementByID("price_FGC")


End Sub

我正在尝试获取HTML表格中的项目的价格。我找到了表的ID,&#34; price_FGC&#34;,但实际价格嵌套在该ID中。我尝试使用.innerText,但它在ie8中不受支持或者我做错了。这是元素树的屏幕截图:

capture

我已经看过一些关于添加&#34;孩子&#34;的例子。某些线路,但没有真实的例子说这种情况。我也知道excel可以抓取表格数据&#34; en masse,&#34;但由于可用资源和可迭代性,我出于特定原因这样做。

解决:  感谢@Nathan_Sav的帮助。发布给有类似问题的其他人,只能访问IE8。纠正变量名称对我的同事更有意义:D

   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 "http://******.aspx?UID=" & item & "~***"
   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

1 个答案:

答案 0 :(得分:1)

添加为答案:

houseprice.getelementsbytagname(“td”)或者也许是“你”

@Nathan_Sav,谢谢你!我设置了一个等于getElementsByTagName(“u”)的新对象,并在VBA中使用我的Locals窗口来查找正确的项目编号。我将用完成的产品编辑我的帖子。