选择divs并使用htmlagilitypack放入集合中不起作用 [英] select divs and put into collection using htmlagilitypack not working
问题描述
为什么这不起作用?在启动时,foreach循环上出现空引用异常错误
Why does this not work? I get a null reference exception error on the foreach loop as it starts
我正在尝试在页面上获取所有div文本,并将每个文本放入我自己的收藏夹中
I'm trying to get all the divs text on a page and put each one into my own collection
Imports HtmlAgilityPack
Imports System.Xml
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim webGet As HtmlWeb = New HtmlWeb
Dim htmlDoc As HtmlDocument = webGet.Load("http://www.mysite.com")
Dim ids As New List(Of String)()
For Each div As Object In htmlDoc.DocumentNode.SelectNodes("//div")
ids.Add(div.InnerText)
Next
End Sub
End Class
例外
exception
对象引用未设置为对象的实例.说明: 当前Web执行期间发生未处理的异常 要求.请查看堆栈跟踪以获取有关以下内容的更多信息: 错误及其在代码中的起源.
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
异常详细信息:System.NullReferenceException:没有对象引用 设置为对象的实例.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
源错误:
第12行:Dim ids作为新列表(字符串)()
第13行:
第14行:对于htmlDoc.DocumentNode.SelectNodes("//div")中的每个div作为对象
第15行:
第16行:ids.Add(div.InnerText)
Line 12: Dim ids As New List(Of String)()
Line 13:
Line 14: For Each div As Object In htmlDoc.DocumentNode.SelectNodes("//div")
Line 15:
Line 16: ids.Add(div.InnerText)
推荐答案
您的代码看起来正确. URL"http://www.mysite.com"可能未返回有效的HTML.
Your code looks correct. Perhaps the URL "http://www.mysite.com" is not returning a valid HTML.
以下代码有效:
Imports HtmlAgilityPack
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim webGet As HtmlWeb = New HtmlWeb
Dim htmlDoc As HtmlDocument = webGet.Load("http://stackoverflow.com/q/11528387/1350308")
Dim ids As New List(Of String)()
TextBox1.Text = ""
For Each div As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//div")
TextBox1.Text += div.Id + Environment.NewLine
Next
End Sub
End Class
TextBox1中的结果是:
The result in TextBox1 is:
noscript-padding
notify-container
overlay-header
custom-header
header
portalLink
topbar
hlinks
hsearch
hlogo
hmenus
content
question-header
mainbar
question
adzerk1
comments-11528387
answers
answers-header
tabs
answer-11528559
comments-11528559
post-editor
wmd-button-bar
draft-saved
draft-discarded
wmd-preview
sidebar
newuser-box
adzerk2
hireme
feed-link
feed-link-text
prettify-lang
footer
footer-menu
footer-sites
footer-flair
svnrev
copyright
noscript-warning
完整源代码: Q11528387WebApp.7z
这篇关于选择divs并使用htmlagilitypack放入集合中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!