使用VBA从HTML页面提取ID /类信息

我前一段时间写了一个Excel VBA中的程序,它可以读取一个HTML页面,并提取关于我的游戏集合的价格的具体信息。 它工作正常,直到一个月前,一切停止工作,我不明白为什么它不工作了,我需要一些帮助来解决这个问题。

所以这里的代码

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)”:访问被拒绝。

有人能帮我吗? 我很新,所以我发现的大多数信息太复杂了,我不明白。

目前,当您尝试将innerHTML文本写入它时,您的代码不会初始化htm对象。 可能,您的post没有显示所有的代码。 考虑使用HTMLFile来保存responsetext ,然后在html对象上运行search:

 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 

最后我解决了我的问题!

  ' 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).innertext

但第二天,那个解决scheme不再工作了,所以我试了以前的一个节目,它工作正常。 通过查看HTML代码,我发现它是不同的,所以可能是因为这是为什么我原来的代码停止工作,我需要改变为WINHTTP.WinHTTPRequest.5.1