vb.net - 获取所有包含“/product/”的链接
问题描述
我想获取所有包含/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
解决方案
您必须先加载网页,然后选择要打印的节点和属性。
这是一种方法:
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。
推荐阅读
- javascript - 散景:使用 TextInput 小部件选择 ColumnDataSource 行
- c# - JsonConvert.DeserializeObject:解开一个对象
- jquery - 替换中的多链
- docker - docker-compose:从 docker 容器访问 localhost
- java - HttpMessageNotReadableException 而不是 BeanPropertyBindingResult
- windows - Win10睡眠后无法删除注册表项值
- javascript - 按预定顺序对一组数字进行排序的最干净和最简单的方法是什么
- python-3.x - 返回检测到相同对象的索引
- r - 使用 data.table 对象生成动态 R-Markdown 块
- python - 内存错误 - Python(大量文件更改)