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

时间:2016-05-15 01:07:13

标签: html excel vba excel-vba

我刚才写了一个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

这是一个示例页面,其中" console"已被nintendo-64和"" name"取代作者:mario-kart-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美元然后,就像我说的那样,它不再起作用了,我得到了错误&#39; -2147024891(80070005)&#39 ;:访问被拒绝。

有人能帮助我吗?我对此非常陌生,所以我发现的大部分信息都太复杂了,我无法理解。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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代码已被修改,因为我第一次成功使其再次使用指令:     单元格(ligne + 1,3).Value = htm.getelementbyid(“price_data”)。getElementsByTagName(“p”)(0).innertext

但是第二天,那个解决方案不再起作用,所以我之前尝试了当前的一个节目并且它工作正常。通过查看HTML代码我注意到它是不同的因此可能是因为我的原始代码停止工作的原因,我需要更改为WINHTTP.WinHTTPRequest.5.1