【问题标题】:vb.net htmlagilitypack loop of selectnode选择节点的vb.net htmlagilitypack循环
【发布时间】:2016-03-11 17:16:45
【问题描述】:

我做了一个链接数组

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

我为每个这样的链接做了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

现在我有了 urls 字符串数组中所有链接的 html 源代码 我想使用 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 链接到urls 中的每个字符串

【问题讨论】:

  • 定义“无效”。如果它是公开的,还发布一个代表 URL。我的猜测是你的“Xpath”的 XPATH 模式是假的——这通常是我的错误。
  • htmlDoc.Load(urls(i))它发送给我路径中的非法字符
  • @rheitzman 和 Xpath 修复了所有链接,因为当我在单个链接中尝试它时它工作得很好

标签: arrays vb.net loops html-agility-pack


【解决方案1】:

您应该使用 HtmlDocument.LoadHtml() 而不是 HtmlDocument.Load(),因为您想从 HTML 字符串中填充 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")

【讨论】:

    【解决方案2】:

    这是我使用的一些库代码:

    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(" ")
    

    【讨论】:

    • 你能帮我如何将SelectNodes("Xpath") 放入循环中,以便为urls 数组中的每个链接源工作
    • WebRequest 工作正常我从 urls 数组中的链接(页面源)中得到了我需要的东西,但我不知道如何在 html-agility-pack DocumentNode.SelectNodes 中使用它
    • 您不需要敏捷包...如果您有要分享的真实 URL 和真实 XPath 目标,我们或许可以提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多