如何使用XPATH或其他技术从VB.net中的网站提取数据?
问题描述:
我想在VB.net中编写一个简单的应用程序来从网站收集信息,然后让应用程序通过电子邮件将结果通过电子邮件发送给我。目的是在更换碳粉时从打印机收集页数。我有我需要的数据XPATH,但我一直无法弄清楚如何在VB中使用它。 (我的编程经验很少)。如何使用XPATH或其他技术从VB.net中的网站提取数据?
到目前为止,我已将应用程序登录到打印机门户并显示具有所需信息的网页。此信息的XPATH是:
// * [@ ID = “内容”]/DIV [3]/DIV /台/ TBODY/TR [1]/TD
谁能帮我提取和从这个表格单元中解析数字?
感谢您的任何帮助,你们可以给!
答
你可以使用streamreader!看看这个例子:
Dim inStream As StreamReader
Dim webRequest As WebRequest
Dim webresponse As WebResponse
webRequest = webRequest.Create("YOUR URL TO THE PAGE GOES HERE")
webresponse = webRequest.GetResponse()
inStream = New StreamReader(webresponse.GetResponseStream())
Dim sourcecode As String = inStream.ReadToEnd()
Dim x1 As Integer = sourcecode.IndexOf("<td>Nr. of toner changed:") + 25 '25 is the number of characters in the string to search
Dim x2 As Integer = sourcecode.IndexOf("times </td>") 'what comes right after your search string. has to be unique in the page
dim nuberofchanges as integer = sourcecode.Substring(x1, x2 - x1)
希望这有助于! :)