Http 使用vbscript从html代码中检索数据
我有一个问题-我有一个URL:like(http://exampl.com/example),当我在浏览器中打开此页面时,我看到一个带有vertain值的表。下面是一个HTML代码:Http 使用vbscript从html代码中检索数据,http,vbscript,Http,Vbscript,我有一个问题-我有一个URL:like(http://exampl.com/example),当我在浏览器中打开此页面时,我看到一个带有vertain值的表。下面是一个HTML代码: <p> <table> <tr><td>RegistrationDTO.setUsername</td> <td>0</td> <td>0</td> <td>1</td> <td
<p>
<table>
<tr><td>RegistrationDTO.setUsername</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr><td>RegistrationDTO.getLastName</td>
<td>0</td>
<td>0</td>
<td>3</td>
<td>3</td>
</tr>
<tr><td>RegistrationDTO.getPersonalNumber</td>
<td>0</td>
<td>0</td>
<td>3</td>
<td>3</td>
</tr><tr>
<td>RegistrationDTO.getFirstName</td>
<td>16</td>
<td>16</td>
<td>3</td>
<td>3</td>
</tr>
<tr><td>RegistrationDTO.register</td>
<td>1068</td>
<td>1068</td>
<td>1</td>
<td>1</td>
</tr>
RegistrationDTO.setUsername
0
0
1.
1.
RegistrationDTO.getLastName
0
0
3.
3.
注册到。getPersonalNumber
0
0
3.
3.
RegistrationDTO.getFirstName
16
16
3.
3.
注册
1068
1068
1.
1.
我需要从这个表的RegistrationDTO.register='1068'(第一个)中获取值
我该怎么做?请帮忙 试试这样的方法:
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://example.com/example"
While ie.Busy : WScript.Sleep 100 : Wend
For Each tr In ie.document.getElementsByTagName("tr")
If InStr(tr.innerText, "RegistrationDTO.register") > 0 Then
Set row = tr
End If
Next
WScript.Echo row.children(1).innerText
ie.Quit
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://example.com/example"
While ie.Busy : WScript.Sleep 100 : Wend
Set fso = CreateObject("Scripting.FileSystemObject")
fso.OpenTextFile("C:\debug.html", 2, True).Write ie.document.body.innerHtml
对于疑难解答,要检查脚本实际看到的内容,请尝试以下操作:
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://example.com/example"
While ie.Busy : WScript.Sleep 100 : Wend
For Each tr In ie.document.getElementsByTagName("tr")
If InStr(tr.innerText, "RegistrationDTO.register") > 0 Then
Set row = tr
End If
Next
WScript.Echo row.children(1).innerText
ie.Quit
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://example.com/example"
While ie.Busy : WScript.Sleep 100 : Wend
Set fso = CreateObject("Scripting.FileSystemObject")
fso.OpenTextFile("C:\debug.html", 2, True).Write ie.document.body.innerHtml
检查
C:\debug.html
是否确实包含有问题的表元素。从html获取信息有两种方法。这两种方法都在这里演示:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Const csURL = "http://gent/16584664.html" ' 'real' const
Dim csFSpec : csFSpec = goFS.GetAbsolutePathName(".\16584664.html") ' 'pseudo' const
' use comment or order to select a strategy
WScript.Quit useDom()
WScript.Quit useRegExp()
Function useRegExp()
Dim sAll : sAll = goFS.OpenTextFile(csFSpec).ReadAll()
Dim reCut : Set reCut = New RegExp
reCut.Global = True
reCut.Pattern = "RegistrationDTO.register</td>\s+<td>(\d+)</td>"
Dim oMTS : Set oMTS = reCut.Execute(sAll)
If 1 = oMTS.Count Then
WScript.Echo "success:", oMTS(0).SubMatches(0)
Else
WScript.Echo "failure"
End If
useRegExp = 0
End Function ' useRegExp
Function useDom()
Dim oDOM : Set oDOM = CreateObject("HTMLFILE")
' Dim sHTML : sHTML = getURL(csURL)
Dim sHTML : sHTML = goFS.OpenTextFile(csFSpec).ReadAll() ' for testing
oDOM.write sHTML
' descending the tree HTML BODY TABLE TBODY 5th row 2nd col
Dim oItem : Set oItem = oDOM.childNodes(0).childNodes(1).childNodes(0).childNodes(0).childNodes(4).childNodes(1)
' WScript.Echo "success:", oItem.tagName ' drill down help
WScript.Echo "success:", oItem.innerHTML
' WScript.Echo "success:", oItem.parentNode.childNodes(2).innerHTML ' verification
useDom = 0
End Function ' useDom
Function getURL(sURL)
Dim oHTTP : Set oHTTP = CreateObject("Msxml2.XMLHTTP")
oHTTP.Open "GET", csURL, False
oHTTP.Send
If 200 = oHTTP.Status Then
getURL = oHTTP.responseText
Else
' handle error
End If
End Function ' getURL
显然,RegExp方法假定您可以定义一个模式来唯一地标识所需的部件。DOM方法无法处理错误的HTML,如果必须按位置获取所需的项(如本例中所示),则可能会很笨拙。两者都对HTML的更改很敏感
p.S.
csFSpec是一个包含给定示例HTML的文件(完整路径),该文件被包装在必要的标记中,以使其合法。对于测试,您可以从浏览器中另存为页面
如果将csURL设置为正确的URL并在useDom()函数中激活getURL()行,则不需要该文件
p.p.S.
为了处理Emil在他的(可能很快会被删除)另一个问题中给出的略有不同的HTML,我不得不将RegExp模式从:
reCut.Pattern = "RegistrationDTO.register</td>\s+<td>(\d+)</td>"
致:
要考虑额外的p和更改的行数
这说明了我上面提到的两种策略的脆弱性@Ansgar在一个(希望如此)稳定的集合上循环并进行比较的方法在这里可能会有所帮助。我需要使用vbscript在两个解决方案中实现这一点,我都遇到了一个错误!可能是因为这个原因:这是一个Java web应用程序。请你帮我一下,我收到一条错误消息!我不知道在csFSpec里放什么:csFSpec!这个页面是用Java制作的!所以它看起来像:localhost:8081/register/monitor?command=getAllStatina在两个解决方案中,我都出现了一个错误!这可能是因为以下原因:这是一个Java web应用程序符合您的解决方案:它在IE中打开我的页面,但随后我得到错误:行:5字符:1未指定的错误代码:80004005源:(null)它写入文件未找到错误抱歉回答时间太长!我尝试了你的代码,并收到了一个错误:第6行字符1未指定的错误。代码80004005来源:空运行上述代码后,浏览器窗口应可见。页面实际加载了吗?当您尝试
ie.document.documentElement.outerHtml
而不是ie.document.body.innerHtml
时,是否会得到相同的结果?WScript.Echo TypeName(ie.document)
的输出是什么?
' descending the tree HTML BODY TABLE TBODY 5th row 2nd col
Dim oItem : Set oItem = oDOM.childNodes(0).childNodes(1).childNodes(0).childNodes(0).childNodes(4).childNodes(1)
' descending the tree HTML BODY P TABLE TBODY 6th row 2nd col
Dim oItem : Set oItem = oDOM.childNodes(0).childNodes(1).childNodes(0).childNodes(0).childNodes(0).childNodes(5).childNodes(2)