【问题标题】:Get all links containing "/product/"获取所有包含“/product/”的链接
【发布时间】:2019-11-23 12:09:29
【问题描述】:

我想获取所有包含/product/ 的链接。有 17 个链接包含 /product/。该怎么做?

这行好像有问题

Dim srcs = From iframeNode In htmlDoc.DocumentNode.SelectNodes("//a[@href]")
                       Select iframeNode.Attributes("href").Value

如何添加参数以通过/product/过滤?

这是我目前所拥有的:

Imports HtmlAgilityPack

Module Module1

    Sub Main()
        Dim mainUrl As String = "https://www.nordicwater.com/products/waste-water/"
        Dim htmlDoc As New HtmlAgilityPack.HtmlDocument

        htmlDoc.LoadHtml(mainUrl)

        Dim srcs = From iframeNode In htmlDoc.DocumentNode.SelectNodes("//a[@href]")
                   Select iframeNode.Attributes("href").Value

        'print all the src you got
        For Each src In srcs
            Console.WriteLine(src)
        Next
    End Sub

End Module

编辑:

工作解决方案:

    Imports HtmlAgilityPack

    Module Module1

        Sub Main()
            Dim mainUrl As String = "https://www.nordicwater.com/products/waste-water/"
            Dim htmlDoc As HtmlDocument = New HtmlWeb().Load(mainUrl) '< - - - Load the webage into htmldocument

            Dim srcs As HtmlNodeCollection = htmlDoc.DocumentNode.SelectNodes("//ul[@class='products-list-page']//a") '< - - - select nodes with links
            For Each src As HtmlNode In srcs
                Console.WriteLine(src.Attributes("href").Value) '< - - - Print urls

            Next

                Console.Read()

        End Sub

    End Module

【问题讨论】:

    标签: vb.net web-scraping web-crawler html-agility-pack


    【解决方案1】:

    您必须先加载网页,然后选择要打印的节点和属性。

    这是一种方法:

        Dim mainUrl As String = "https://www.nordicwater.com/products/waste-water/"
        Dim htmlDoc As HtmlDocument = New HtmlWeb().Load(mainUrl) '< - - - Load the webage into htmldocument
    
        Dim srcs As HtmlNodeCollection = htmlDoc.DocumentNode.SelectNodes("//ul[@class='products-list-page']//a") '< - - - select nodes with links
        For Each src As HtmlNode In srcs
            Console.WriteLine(src.Attributes("href").Value) '< - - - Print urls
        Next
    

    您需要学习调试,如果您检查过代码,您会发现您正在将“htmlDoc”html 设置为 url 字符串,而不是加载实际的网页 html。

    【讨论】:

    • 谢谢你!我在Console.WriteLine 之后添加了Console.Read(),结果我只看到一个链接。第一个。有一个循环,但为什么它不起作用?
    • 您需要指定更多错误在哪里,因为这对我来说很好,请编辑您的问题。
    • 运行代码后,我在控制台中得到“nordicwater.com/product/mrs-meva-multi-rake-screen
    • 发布您现在拥有的所有代码,然后看看。这样的猜测没有任何意义。
    • 对不起,我的错!我在循环 damn 中添加了Console.Read()
    猜你喜欢
    • 2014-09-23
    • 2021-03-11
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2021-01-23
    • 2015-08-19
    • 2023-03-20
    • 2020-08-22
    相关资源
    最近更新 更多