2016-05-15 44 views
1

我之前寫過一個Excel VBA中的程序,它可以讀取HTML頁面並提取有關我的遊戲集合價格的特定信息。它工作正常,直到一個月前,一切都停止工作,我不明白爲什麼它不再工作,我需要一些幫助來解決這個問題。使用VBA從HTML頁面提取ID /類信息

所以這裏的代碼

Dim htm As Object   ' tableau HTML venant du site pricecharting 
    ' Find price over internet 
    With CreateObject("msxml2.xmlhttp") 
     .Open "GET", "http://videogames.pricecharting.com/game/" & console & "/" & name & "", False 
     .Send 
     htm.body.innerhtml = .responsetext 
    End With 

    ' what I want is in a table row 1 and cell 0 (used price) 
    Cells(ligne + 1, 3).Value = htm.getelementbyid("price_data").Rows(1).Cells(0).innerText 

和這裏是 「控制檯」 已換成一個示範頁面任天堂-64和 「名」 馬里奧卡丁車-64:

https://www.pricecharting.com/game/nintendo-64/mario-kart-64

<div id="price_data" class="info_box"> 
     <div id="used_price"> 
      <h3>Loose <span>Price</span></h3> 
      <p class="price"> 
     $44.60 
      </p> 
      <p class="js-show-tab volume" data-show-tab="completed-auctions-used"> 
    <span class="tablet-portrait-hidden">Volume:&nbsp;</span> 
    <a href="#">3 sales per day</a> 
</p> 
     </div> 

     <div id="complete_price"> 
      <h3>Complete <span>Price</span></h3> 
      <p class="price"> 
     $65.99 
      </p> 
      <p class="js-show-tab volume" data-show-tab="completed-auctions-cib"> 
    <span class="tablet-portrait-hidden">Volume:&nbsp;</span> 
    <a href="#">2 sales per week</a> 
</p> 
     </div> 

     <div id="new_price"> 
      <h3>New <span>Price</span></h3> 
      <p class="price"> 


     $178.14 


      </p> 
      <p class="js-show-tab volume" data-show-tab="completed-auctions-new"> 
    <span class="tablet-portrait-hidden">Volume:&nbsp;</span> 
    <a href="#">2 sales per month</a> 
</p> 
     </div> 
</div> 

我的目標(我成功了)是去提取價格44.60美元,但後來,就像我說的,它不再工作,我得到錯誤'-2147024891(80070005)':訪問被拒絕。

有人可以幫助我嗎?我在這方面很新,所以我發現的大部分信息都太複雜了,以至於我無法理解。

回答

0

當您試圖將innerhtml文本寫入它時,您的代碼不會初始化htm對象。可能,您的帖子沒有顯示所有代碼。考慮使用HTMLFile舉行responsetext然後將HTML對象上運行搜索:

Public Sub WebScrape() 
    Dim htm As Object, doc As Object 

    Set htm = CreateObject("MSXML2.XMLHTTP") 
    Set doc = CreateObject("HTMLFile") 

    With htm 
     .Open "GET", "http://videogames.pricecharting.com/game/" _ 
         & console & "/" & Name & "", False 
     .Send 
     doc.Open 
     doc.write .responsetext 
     doc.Close 
    End With 

    Cells(ligne + 1, 3).Value = doc.getelementbyid("price_data").Rows(1).Cells(0).innerText 

    Set doc = Nothing 
    Set htm = Nothing 

End Sub 
+0

它是我用來提取信息的所有代碼我需要。我嘗試了你的解決方案,但它不起作用,程序仍停在「.send」命令並彈出錯誤消息。是否可能是在可能的更新中,請求執行我想要的任務的「包」MSXML2.XMLHTTP已被阻止? –

0

最後我解決我的問題!

' Search price over internet 
    With CreateObject("WINHTTP.WinHTTPRequest.5.1") 
     .Open "GET", "http://videogames.pricecharting.com/game/" & console & "/" & name & "", False 
     .Send 
     htm.body.innerhtml = .responsetext 
    End With 

    ' Search the price with the state of the game (completed ou cartridge only) 
    If consoleandname(3) <> "Completed set" Then 

     ' the information we search is found with ID used_price 
     Cells(ligne + 1, 3).Value = htm.getelementbyid("used_price").innertext 

    ElseIf consoleandname(3) = "Completed set" Then 

     ' the information we search is found with ID completed_price 
     Cells(ligne + 1, 3).Value = htm.getelementbyid("complete_price").innertext 
    End If 

因此而不是使用MSXML2.XMLHTTP的,我現在用WINHTTP.WinHTTPRequest.5.1(但我不太清楚有什麼區別)。我發現HTML代碼已被修改,因爲我第一次成功使其再次與指令一起工作: Cells(ligne + 1,3).Value = htm.getelementbyid(「price_data」)。getElementsByTagName(「p 「)(0).Innertextxt

但第二天,該解決方案不再工作了,所以我嘗試了目前的一個節目,它工作正常。通過查看HTML代碼,我注意到它是不同的,所以mayby這就是爲什麼我的原始代碼停止工作,我需要改變WINHTTP.WinHTTPRequest.5.1