无法阅读多个网页

时间:2015-09-20 10:57:15

标签: html excel vba excel-vba web-scraping

我想从这两个相似的页面中获取信息:

http://www.quotenet.com/index/market-movers/S&P_500
http://www.quotenet.com/index/market-movers/CAC_40

我已经有一个适用于一个页面的代码(我想在表格中获得前两个推动者):

Dim ie As InternetExplorer, doc As HTMLDocument
Dim TopMoverTable As Object
Dim TopMoverTable2 As Object



Set ie = CreateObject("InternetExplorer.Application")


ie.navigate "http://www.quotenet.com/index/market-movers/S&P_500"

       Do
       DoEvents
       Loop Until ie.readyState = READYSTATE_COMPLETE

       Set doc = ie.document

      Set TopMoverTable = doc.getElementsByTagName("TABLE")(2)


Cells(1, 2) = Split(TopMoverTable.getElementsByTagName("TD")(0).innerText, vbCrLf)(0)
Cells(1, 3) = Split(TopMoverTable.getElementsByTagName("TD")(4).innerText, vbCrLf)(1)




Cells(2, 2) = Split(TopMoverTable.getElementsByTagName("TD")(11).innerText, vbCrLf)(0)
Cells(2, 3) = Split(TopMoverTable.getElementsByTagName("TD")(15).innerText, vbCrLf)(1)

'ie.navigate "http://www.quotenet.com/index/market-movers/CAC_40"
'       Do
'       DoEvents
'       Loop Until ie.readyState = READYSTATE_COMPLETE
'
'       Set doc = ie.document
'
'      Set TopMoverTable2 = doc.getElementsByTagName("TABLE")(2)


ie.Application.Quit

我评论了无效的代码。

我怎么能阅读这两页?

1 个答案:

答案 0 :(得分:1)

您的代码中缺少 TopMoverTable2 。现在,我操纵了你的代码。

尝试以下代码

Dim ie As InternetExplorer, doc As HTMLDocument
Dim TopMoverTable As Object
Dim TopMoverTable2 As Object

Set ie = CreateObject("InternetExplorer.Application")

ie.navigate "http://www.quotenet.com/index/market-movers/S&P_500"

   Do
   DoEvents
   Loop Until ie.readyState = READYSTATE_COMPLETE

   Set doc = ie.document

   Set TopMoverTable = doc.getElementsByTagName("TABLE")(2)


Cells(1, 2) = Split(TopMoverTable.getElementsByTagName("TD")  (0).innerText, vbCrLf)(0)
Cells(1, 3) = Split(TopMoverTable.getElementsByTagName("TD")(4).innerText, vbCrLf)(1)

Cells(2, 2) = Split(TopMoverTable.getElementsByTagName("TD")(11).innerText, vbCrLf)(0)
Cells(2, 3) = Split(TopMoverTable.getElementsByTagName("TD")(15).innerText, vbCrLf)(1)


ie.navigate "http://www.quotenet.com/index/market-movers/CAC_40"
   Do
   DoEvents
   Loop Until ie.readyState = READYSTATE_COMPLETE

   Set doc = ie.document

  Set TopMoverTable2 = doc.getElementsByTagName("TABLE")(2)


Cells(5, 2) = Split(TopMoverTable2.getElementsByTagName("TD")(0).innerText, vbCrLf)(0)
Cells(5, 3) = Split(TopMoverTable2.getElementsByTagName("TD")(4).innerText, vbCrLf)(1)

Cells(6, 2) = Split(TopMoverTable2.getElementsByTagName("TD")(11).innerText, vbCrLf)(0)
Cells(6, 3) = Split(TopMoverTable2.getElementsByTagName("TD")(15).innerText, vbCrLf)(1)

ie.Application.Quit