【问题标题】:How to get a URL from the href attribute如何从 href 属性中获取 URL
【发布时间】:2017-12-26 21:24:22
【问题描述】:

我无法从 href 属性中获取 URL。 我用这个代码

Dim url As String = "http://example.com/"
Dim web As New HtmlWeb()
Dim doc As HtmlDocument = web.Load(url)

For Each item As HtmlNode In doc.DocumentNode.SelectNodes("//a/@href")
    If Not item Is Nothing Then
        Response.Write(item.OuterHtml)
    End If
Next

但它不起作用。

【问题讨论】:

标签: vb.net xpath html-agility-pack


【解决方案1】:

由于href 是一个属性,您需要将其放在方括号中[]

请记住,当您按属性进行搜索时,它们会放在方括号中。

//a[@href]

在您的情况下,您需要获取所有//a 节点,然后检查HasAttributes("href"),最后获取Attributes("href")

For Each item As HtmlNode In doc.DocumentNode.SelectNodes("//a")
    If Not item Is Nothing And item.HasAttributes("href") Then
        Response.Write(item.Attributes("href").Value)
    End If
Next

【讨论】:

    猜你喜欢
    • 2022-06-22
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2014-10-20
    • 2016-06-07
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    相关资源
    最近更新 更多