vb.net selectnode 的 htmlagilitypack 循环
vb.net htmlagilitypack loop of selectnode
我制作了一个 link 的数组
Dim horof As String = "A B C D"
Dim alphabarray As String() = horof.Split(New Char() {" "c})
Dim urls() As String = alphabarray.Select(Function(o) "http://somelink/list-" & o).ToArray()
输出是这样的
http://somelink/list-A
http://somelink/list-B
http://somelink/list-C
http://somelink/list-D
我为每个 link 做了 webrequest
像这样:
For i As Int32 = 0 To urls.Length - 1
Dim wRequest As WebRequest
Dim WResponse As WebResponse
wRequest = FtpWebRequest.Create(urls(i))
WResponse = wRequest.GetResponse
Dim SR As StreamReader
SR = New StreamReader(WResponse.GetResponseStream)
urls(i) = SR.ReadToEnd
Next
现在我有 html 字符串数组中所有 link 的来源,即 urls
我想使用 htmlagilitypack 来 selectnodes
来自数组中的每个 html 源
Dim htmlDoc As New HtmlDocument()
htmlDoc.LoadHtml(urls)
Dim wantednode = htmlDoc.DocumentNode.SelectNodes("Xpath")
但是没用
我试着把它放在同一个循环中
Dim htmlDoc As New HtmlDocument()
Dim wantednode As HtmlNodeCollection
For i As Int32 = 0 To urls.Length - 1
Dim wRequest As WebRequest
Dim WResponse As WebResponse
wRequest = FtpWebRequest.Create(urls(i))
WResponse = wRequest.GetResponse
Dim SR As StreamReader
SR = New StreamReader(WResponse.GetResponseStream)
urls(i) = SR.ReadToEnd
htmlDoc.Load(urls(i))
wantednode = htmlDoc.DocumentNode.SelectNodes("Xpath")
next
这个也没用
如何制作 wantednode = htmlDoc.DocumentNode.SelectNodes("Xpath")
的循环
对于 urls
数组
中的每个 html 代码
urls
数组中的每个 html 代码都是这样的,来自
<body>
<div class="list_body">
<ul class="listing">
<li>
<a href="http://wanted1.com" title="">title1 </a>
</li>
<li>
<a href="http://wanted2.com" title="">title2 </a>
</li>
<li>
<a href="http://wanted3.com" title="">title3 </a>
</li>
<li>
<a href="http://wanted4.com" title="">title4 </a>
</li>
<li>
<a href="http://wanted5.com" title="">title5 </a>
</li>
<li>
<a href="http://wanted6.com" title="">title6 </a>
</li>
</ul>
</div>
</body>
我希望 http://wanted2.com
link 在 urls
的每个字符串中
这是我使用的一些库代码:
Public Function Web_Request_Response(URL As String) As String
Try
Dim myRequest As HttpWebRequest
Dim myResponse As HttpWebResponse
Dim sr As StreamReader
Dim sResponse As String = ""
myRequest = CType(WebRequest.Create(URL), HttpWebRequest)
myResponse = CType(myRequest.GetResponse(), HttpWebResponse)
sr = New StreamReader(myResponse.GetResponseStream())
sResponse = sr.ReadToEnd.ToString
Return sResponse
Catch ex As Exception
LogMsgBox(ex, ex.Message, , "WebRequest_Responce Error")
Return ""
End Try
End Function
与您的通话略有不同。也许与类型转换有关? FTP 与 HTTP?
很好地使用了 Linq。
您可以节省一些输入时间:
Dim alphabarray As String() = horof.Split(New Char() {" "c})
--- same as ---
Dim alphabarray As String() = horof.Split({" "c})
or
Dim alphabarray As String() = horof.Split(" ")
你应该使用 HtmlDocument.LoadHtml()
而不是 HtmlDocument.Load()
,因为你想从 [=18] 填充 HtmlDocument
=] 字符串:
'urls(i) value has been replaced with HTML string by the following line..
urls(i) = SR.ReadToEnd
'..so next, you need to use `LoadHtml()`
htmlDoc.LoadHtml(urls(i))
wantednode = htmlDoc.DocumentNode.SelectNodes("Xpath")
我制作了一个 link 的数组
Dim horof As String = "A B C D"
Dim alphabarray As String() = horof.Split(New Char() {" "c})
Dim urls() As String = alphabarray.Select(Function(o) "http://somelink/list-" & o).ToArray()
输出是这样的
http://somelink/list-A
http://somelink/list-B
http://somelink/list-C
http://somelink/list-D
我为每个 link 做了 webrequest
像这样:
For i As Int32 = 0 To urls.Length - 1
Dim wRequest As WebRequest
Dim WResponse As WebResponse
wRequest = FtpWebRequest.Create(urls(i))
WResponse = wRequest.GetResponse
Dim SR As StreamReader
SR = New StreamReader(WResponse.GetResponseStream)
urls(i) = SR.ReadToEnd
Next
现在我有 html 字符串数组中所有 link 的来源,即 urls
我想使用 htmlagilitypack 来 selectnodes
来自数组中的每个 html 源
Dim htmlDoc As New HtmlDocument()
htmlDoc.LoadHtml(urls)
Dim wantednode = htmlDoc.DocumentNode.SelectNodes("Xpath")
但是没用
我试着把它放在同一个循环中
Dim htmlDoc As New HtmlDocument()
Dim wantednode As HtmlNodeCollection
For i As Int32 = 0 To urls.Length - 1
Dim wRequest As WebRequest
Dim WResponse As WebResponse
wRequest = FtpWebRequest.Create(urls(i))
WResponse = wRequest.GetResponse
Dim SR As StreamReader
SR = New StreamReader(WResponse.GetResponseStream)
urls(i) = SR.ReadToEnd
htmlDoc.Load(urls(i))
wantednode = htmlDoc.DocumentNode.SelectNodes("Xpath")
next
这个也没用
如何制作 wantednode = htmlDoc.DocumentNode.SelectNodes("Xpath")
的循环
对于 urls
数组
urls
数组中的每个 html 代码都是这样的,来自
<body>
<div class="list_body">
<ul class="listing">
<li>
<a href="http://wanted1.com" title="">title1 </a>
</li>
<li>
<a href="http://wanted2.com" title="">title2 </a>
</li>
<li>
<a href="http://wanted3.com" title="">title3 </a>
</li>
<li>
<a href="http://wanted4.com" title="">title4 </a>
</li>
<li>
<a href="http://wanted5.com" title="">title5 </a>
</li>
<li>
<a href="http://wanted6.com" title="">title6 </a>
</li>
</ul>
</div>
</body>
我希望 http://wanted2.com
link 在 urls
这是我使用的一些库代码:
Public Function Web_Request_Response(URL As String) As String
Try
Dim myRequest As HttpWebRequest
Dim myResponse As HttpWebResponse
Dim sr As StreamReader
Dim sResponse As String = ""
myRequest = CType(WebRequest.Create(URL), HttpWebRequest)
myResponse = CType(myRequest.GetResponse(), HttpWebResponse)
sr = New StreamReader(myResponse.GetResponseStream())
sResponse = sr.ReadToEnd.ToString
Return sResponse
Catch ex As Exception
LogMsgBox(ex, ex.Message, , "WebRequest_Responce Error")
Return ""
End Try
End Function
与您的通话略有不同。也许与类型转换有关? FTP 与 HTTP?
很好地使用了 Linq。
您可以节省一些输入时间:
Dim alphabarray As String() = horof.Split(New Char() {" "c})
--- same as ---
Dim alphabarray As String() = horof.Split({" "c})
or
Dim alphabarray As String() = horof.Split(" ")
你应该使用 HtmlDocument.LoadHtml()
而不是 HtmlDocument.Load()
,因为你想从 [=18] 填充 HtmlDocument
=] 字符串:
'urls(i) value has been replaced with HTML string by the following line..
urls(i) = SR.ReadToEnd
'..so next, you need to use `LoadHtml()`
htmlDoc.LoadHtml(urls(i))
wantednode = htmlDoc.DocumentNode.SelectNodes("Xpath")