在vb.net中,我可以将网页下载为这样的字符串:
Using ee As New System.Net.WebClient()
Dim reply As String = ee.DownloadString("https://pastebin.com/eHcQRiff")
MessageBox.Show(reply)
End Using
是否可以在网页上指定项目的ID标签,以便回复只输出代码框/ id标签内的信息?
示例:
RAW Paste Data
上https://pastebin.com/eHcQRiff
的ID标记为id="paste_code"
,其中包含以下文字:
Test=1
Test=2
有没有让WebClient
仅使用ID标签(或任何其他方法)输出完全相同的消息?
答案 0 :(得分:1)
您可以使用HtmlAgilityPack库
Dim document as HtmlAgilityPack.HtmlDocument = new HtmlAgilityPack.HtmlDocument()
document.Load(@"C:\YourDownloadedHtml.html")
Dim text as string = document.GetElementbyId("paste_code").InnerText
更多示例代码:
(使用HtmlAgilityPack 1.6.10.0测试)
Dim html As string = "<TD width=""""50%""""><DIV align=right>Name :<B> </B></DIV></TD><TD width=""""50%""""><div id='i1'>SomeText</div></TD><TR vAlign=center>"
Dim htmlDoc As HtmlDocument = New HtmlDocument
htmlDoc.LoadHtml(html) 'To load from html string directly
Dim name As String = htmlDoc.DocumentNode.SelectSingleNode("//td/div[@id='i1']").InnerText
Console.WriteLine(name)
输出:
SomeText