【问题标题】:VBA to scrape the value from a hidden fieldVBA从隐藏字段中刮取值
【发布时间】:2018-11-02 23:41:00
【问题描述】:

我是抓取 HTML 的新手,几天来一直在尝试使用类名和 id 从隐藏字段中抓取值,但我仍然无法获取值。

我正在尝试从以下 HTML 中获取值 (4);

<input id="collectionQuantity" type="hidden" value="4">

这是从下面较大的摘录中摘录的;

<div class="lg-24 md-12 cols">
					
	<input id="selectedBranchCode" type="hidden" value="OT4">
	<input id="selectedBranchName" type="hidden" value="Ashton-under-Lyne">
	<input id="collectionQuantity" type="hidden" value="4">
						
		<button id="add_for_collection_button_3730P" title="Click here to add this item to your basket for collection" class="btn btn--lg btn--col fill " data-content="add-to-basket">Click &amp; Collect</button>
		<p id="branch_collection_3730P">4 in stock in <strong>Ashton-under-Lyne</strong> <a href="https://www.screwfix.com/jsp/cpc/cpcCheckStock.jsp?product_id=3730P" id="click_and_collect_3730P" class="_btn--link">Change store</a></p>
													
				</div>

我已经尝试了很多获得价值的方法。 我认为我最亲近的是;

sh01.Cells(r, 5) = HTML.getElementsByClassName("lg-24 md-12 cols")(3).innertext                                  'product stock
sh01.Cells(r, 5) = HTML.getElementsByTagName("p")(7).innertext                                                   'product stock
sh01.Cells(r, 5) = HTML.getElementById("branch_collection_" & z_sh01.Cells(y, 2)).innertext                      'product stock
sh01.Cells(r, 5) = HTML.getElementsByClassName("lg-24 md-12 cols")(3).getElementById("collectionQuantity").Value 'product stock
sh01.Cells(r, 5) = HTML.querySelector("# branch_collection_" & z_sh01.Cells(y, 2)).innertext                     'product stock
sh01.Cells(r, 5) = HTML.getElementById("collectionQuantity").innertext                                           'product stock

提前感谢您的帮助。

伊恩

【问题讨论】:

    标签: html excel vba web-scraping scrape


    【解决方案1】:

    试试

    HTML.querySelector("#collectionQuantity").Value
    

    或者

    HTML.getElementById("collectionQuantity").getAttribute("value")
    

    甚至

    HTML.getElementById("collectionQuantity").Value
    

    您在目标元素的 value 属性值而不是 .innerText 之后。上面显示了 3 种方法来做到这一点。


    所以,我必须设置一个本地存储,然后首先添加一个不同的导航,以确保在转到感兴趣的页面之前设置本地存储,否则存储是空白的,因此有问题的元素的 .value也是空白

    Option Explicit
    
    Public Sub GetInfo()
        Dim IE As New InternetExplorer
        With IE
            .Visible = True
            .Navigate2 "https://www.screwfix.com/jsp/tradeCounter/tradeCounterDetailsPage.jsp?id=460"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            .document.querySelector("input.btn").Click
    
            While .Busy Or .readyState < 4: DoEvents: Wend
            .Navigate2 "https://www.screwfix.com/p/hd907-9q-freestanding-oil-filled-radiator-2000w/3730p"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            With .document
    
                Debug.Print .getElementById("collectionQuantity").Value
    
            End With
    
            .Quit
        End With
    End Sub
    

    【讨论】:

    • 谢谢,但不幸的是两者都不起作用。我追求价值的价值。 - 在上述情况下,4. 再次感谢。杨格
    • 不工作是什么意思?我将您的 HTML 放入文档中,它运行良好。你能提供网页的网址吗?还是更多 HTML ?例如,是否有父 iframe/frame ?
    • 实际上,我认为它是错误处理中的代码,但它用空白填充它。 url 是 screwfix.com/p/hd907-9q-freestanding-oil-filled-radiator-2000w/… 并且值应该读为“0”,因为您没有选择本地商店。谢谢。
    • 有网址可以分享吗?
    • 绝对令人惊叹的东西!!!谢谢你。你拯救了我剩下的最后一点头发。谢谢你。伊恩·G。
    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2013-12-30
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多