如果我打电话
=GetElementByRegex(A3,"placeholder=""(.*?)"">")
它可以工作,但是当我想输入数字或其他代码时
A2=`https://www.alexa.com/siteinfo/taiwantrade.com/`
=GetElementByRegex(A2,"<!-- Alexa web traffic metrics are available via our API at http://aws.amazon.com/awis -->(.*?)</strong>")
它变成空的。我不知道为什么。我是新来的
我正在使用的代码。
Public Function GetElementByRegex(url As String, reg As String)
Dim XMLHTTP As Object, html As Object, objResult As Object
Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.ResponseText
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = reg
regEx.Global = True
If regEx.Test(XMLHTTP.ResponseText) Then
Set matches = regEx.Execute(XMLHTTP.ResponseText)
If IsMissing(index) Then
GetElementByRegex = matches(0).SubMatches(0)
Else
GetElementByRegex = matches(index).SubMatches(0)
End If
Exit Function
End If
GetElementByRegex = ""
End Function
答案 0 :(得分:0)
使用以下正则表达式:https://regex101.com/r/EQBjSC/1
=GetElementByRegex(A2,"<!-- Alexa web traffic metrics are available via our API at http:\/\/aws\.amazon\.com\/awis -->\n(.*?)<\/strong>")
请注意,您必须使用反斜杠/
来转义.
和\
之类的特殊字符