Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从HTML源中提取值_Html_Excel_Vba_Internet Explorer_Extract - Fatal编程技术网

从HTML源中提取值

从HTML源中提取值,html,excel,vba,internet-explorer,extract,Html,Excel,Vba,Internet Explorer,Extract,我有一个宏,用于访问网站,从代码的特定部分从a列中提取一个值,例如517167000,然后将该值返回到单元格。 html源代码现在已经更改,我似乎无法让它工作 我的原始代码是 Public Function UnitPerBox(searchTerm As String) As String Static request As Object If request Is Nothing Then Set request = CreateObject("msxml2.xmlhttp") With

我有一个宏,用于访问网站,从代码的特定部分从a列中提取一个值,例如517167000,然后将该值返回到单元格。 html源代码现在已经更改,我似乎无法让它工作

我的原始代码是

Public Function UnitPerBox(searchTerm As String) As String
Static request As Object
If request Is Nothing Then Set request = CreateObject("msxml2.xmlhttp")

With request
    .Open "GET", "https://larsonjuhl.co.uk/mouldings/larson-juhl-essentials/arq-essentials-moulding-" & searchTerm, False
    .send
    UnitPerBox = Trim(Split(Split(.responseText, "Units per box</td>")(1), "<tr")(0))
End With

End Function
Public Function UnitPerBox(searchTerm作为字符串)作为字符串
作为对象的静态请求
如果请求为Nothing,则设置request=CreateObject(“msxml2.xmlhttp”)
请求
.打开“获取”https://larsonjuhl.co.uk/mouldings/larson-juhl-essentials/arq-essentials-moulding-“&searchTerm,False”
.发送

UnitPerBox=Trim(Split(Split(.responseText,“Units per box”)(1),“如果您使用
。responseText
使用
Split()
执行文本操作,您还可以使用正则表达式,而无需设置其
全局
参数:

Public Function UnitPerBox(searchTerm As String) As String
Static request As Object
If request Is Nothing Then Set request = CreateObject("msxml2.xmlhttp")

Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
RegEx.Pattern = "\d+(?:\.\d+)?"

With request
    .Open "GET", "https://larsonjuhl.co.uk/mouldings/larson-juhl-essentials/arq-essentials-moulding-" & searchTerm, False
    .send
    UnitPerBox = RegEx.Execute(Split(.responsetext, "Units per pack</td>")(1))(0)
End With

End Function
请注意,该表是0索引的,这意味着我们实际上是从第11行第二列检索值。如果您不确定tablecontent是否总是在相同的索引上找到,您也可以只循环子节点:

Public Function UnitPerBox(searchTerm As String) As String
Static request As Object
If request Is Nothing Then Set request = CreateObject("msxml2.xmlhttp")
Dim htmlResponse As Object: Set htmlResponse = CreateObject("htmlfile")
Dim Rws As Object

With request
    .Open "GET", "https://larsonjuhl.co.uk/mouldings/larson-juhl-essentials/arq-essentials-moulding-" & searchTerm, False
    .send
    htmlResponse.body.innerHTML = .responseText
    Set Rws = htmlResponse.body.document.getElementById("specifications").getElementsByTagName("tr")
    For Each Rw In Rws
        If Rw.getElementsByTagName("td")(0).InnerText = "Units per pack" Then
            UnitPerBox = Rw.getElementsByTagName("td")(1).InnerText
            Exit For
        End If
    Next
End With

End Function

我个人更喜欢使用
HTML
文档而不是文本操作,以上所有选项都可以检索您的值=)

非常感谢。为什么在这个UDF中使用Static?我的意思是它有什么好处?@Yasserkalil,看看这个,我不确定这里的
Static
的优势有多大,因为我不知道设置request=CreateObject(“msxml2.xmlhttp”)需要多少资源
。如果你打算大量使用该代码,我想它会有一些优势,因为它可以防止反复创建此对象……嗨,JvdV,感谢您的快速响应。我对这一点非常陌生,但我尝试了你的所有三个答案,我在单元格中得到了一个值错误。我在单元格中的公式实际上是=UnitPerBox(A1)举个例子。这不正确吗?它考虑了一会儿,所以它肯定在做一些事情,但我没有得到返回的值。再次感谢你的帮助。我尝试了所有3个。仍然得到那个错误,显然我做错了。这可能真的很无礼,你能附上你的工作表吗。我的工作表包含p价格和折扣等。谢谢again@JvdV我接受你的回答,但结果是我甚至不需要帮助。我使用的是macbook pro,显然macOS不喜欢。我登录到我的windows远程桌面,我的旧桌面工作正常:)再次感谢
<tr>
                <td class="name">Units per pack</td>
                <td class="value">2.74</td>
            </tr>
Public Function UnitPerBox(searchTerm As String) As String
Static request As Object
If request Is Nothing Then Set request = CreateObject("msxml2.xmlhttp")

Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
RegEx.Pattern = "\d+(?:\.\d+)?"

With request
    .Open "GET", "https://larsonjuhl.co.uk/mouldings/larson-juhl-essentials/arq-essentials-moulding-" & searchTerm, False
    .send
    UnitPerBox = RegEx.Execute(Split(.responsetext, "Units per pack</td>")(1))(0)
End With

End Function
Public Function UnitPerBox(searchTerm As String) As String
Static request As Object
If request Is Nothing Then Set request = CreateObject("msxml2.xmlhttp")
Dim htmlResponse As Object: Set htmlResponse = CreateObject("htmlfile")

With request
    .Open "GET", "https://larsonjuhl.co.uk/mouldings/larson-juhl-essentials/arq-essentials-moulding-" & searchTerm, False
    .send
    htmlResponse.body.innerHTML = .responseText
    UnitPerBox = htmlResponse.body.document.getElementById("specifications").getElementsByTagName("tr")(10).getElementsByTagName("td")(1).innerText
End With

End Function
Public Function UnitPerBox(searchTerm As String) As String
Static request As Object
If request Is Nothing Then Set request = CreateObject("msxml2.xmlhttp")
Dim htmlResponse As Object: Set htmlResponse = CreateObject("htmlfile")
Dim Rws As Object

With request
    .Open "GET", "https://larsonjuhl.co.uk/mouldings/larson-juhl-essentials/arq-essentials-moulding-" & searchTerm, False
    .send
    htmlResponse.body.innerHTML = .responseText
    Set Rws = htmlResponse.body.document.getElementById("specifications").getElementsByTagName("tr")
    For Each Rw In Rws
        If Rw.getElementsByTagName("td")(0).InnerText = "Units per pack" Then
            UnitPerBox = Rw.getElementsByTagName("td")(1).InnerText
            Exit For
        End If
    Next
End With

End Function