VB.NET ~ 我修复了我的应用程序,为我提供网站的所有 img src 标签。但我需要的一个标签没有在结果中列出
我需要抓取的 img src 是:
div id="recaptcha_image" class="width: 300px; height: 57px;" style="width: 300px; height: 57px;">
<img width="300" height="57" src="http://www.google.com/recaptcha/api/image?c=03AHJ_VuvlvMA4JvVIQvDR4C_iDbOTwOF5FUIRPGkkSImDRYAD6sY2L0IxyJSpSP1WGjWqr0MQ-dmjkiIgevFY2gkMpNWi1cQbtgUZB5QaYr_vIHv6xFzG9ydFbBWs4xiEhWoxHEFUYHZj6CCh4obyZSOd2La0nozLZw" style="display:block;">
这是我的代码,可以从任何网站抓取所有 img src 标签。一件事是我需要的 img src 没有在返回的结果中列出。
如何更正我的代码以仅获取这一字段?这是我的工作程序...目前不会将图片加载到图片框中...但它确实在富文本框中返回结果。谢谢
Dim s As String = TextBox1.Text
Dim hw As New HtmlWeb()
Dim doc As HtmlDocument = hw.Load(s)
Dim items As HtmlNodeCollection = doc.DocumentNode.SelectNodes("//img")
If items Is Nothing Then
MessageBox.Show("There is nothing to show you")
End If
If items IsNot Nothing Then
For Each item As HtmlNode In items
RichTextBox2.Text = RichTextBox2.Text & (item.GetAttributeValue("src", "value") & Environment.NewLine)
'PictureBox1.Load(item.GetAttributeValue("src", TextBox1.Text & "value"))
Next
The img src that I need to grab is:
div id="recaptcha_image" class="width: 300px; height: 57px;" style="width: 300px; height: 57px;">
<img width="300" height="57" src="http://www.google.com/recaptcha/api/image?c=03AHJ_VuvlvMA4JvVIQvDR4C_iDbOTwOF5FUIRPGkkSImDRYAD6sY2L0IxyJSpSP1WGjWqr0MQ-dmjkiIgevFY2gkMpNWi1cQbtgUZB5QaYr_vIHv6xFzG9ydFbBWs4xiEhWoxHEFUYHZj6CCh4obyZSOd2La0nozLZw" style="display:block;">
Here is my code that grabs all the img src tags from any website. The one thing is the img src that I need isn't listed in the returned results.
How can I correct my code to grab only this one field? Here is my working program...currently is doesn't load a pic into the picturebox...but it does return the results in the rich text box. Thanks
Dim s As String = TextBox1.Text
Dim hw As New HtmlWeb()
Dim doc As HtmlDocument = hw.Load(s)
Dim items As HtmlNodeCollection = doc.DocumentNode.SelectNodes("//img")
If items Is Nothing Then
MessageBox.Show("There is nothing to show you")
End If
If items IsNot Nothing Then
For Each item As HtmlNode In items
RichTextBox2.Text = RichTextBox2.Text & (item.GetAttributeValue("src", "value") & Environment.NewLine)
'PictureBox1.Load(item.GetAttributeValue("src", TextBox1.Text & "value"))
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有完整的 HTML,很难说,但如果您想要此特定
img
的src
,您可以在SelectNodes
中使用以下内容(其中可能应该更改为SelectSingleNode
)。doc.DocumentNode.SelectSingleNode("//div[@id='recaptcha_image']/img[1]").Attributes("src").Value
上面将返回
src
img
标签的字符串。Without the complete HTML it is difficult to say, but if you want the
src
for this specificimg
you can use the following inSelectNodes
(which should probably be changed toSelectSingleNode
).doc.DocumentNode.SelectSingleNode("//div[@id='recaptcha_image']/img[1]").Attributes("src").Value
The above will return the
src
String for theimg
tag.