Html 选择 C#
我的源代码中有多个如下所示的元素。
<a id="thread_title_158922" href="#"></a>
问题是我不知道标题后面的数字。 所以我尝试了这个语法:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(code);
var items = doc.DocumentNode.SelectNodes("//a[@id='thread_title_*]");
但它不起作用。有解决办法吗?
I have multiple elements in the sourcecode which look like this.
<a id="thread_title_158922" href="#"></a>
The problem is that i don't know the number behind title.
So i tried this syntax:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(code);
var items = doc.DocumentNode.SelectNodes("//a[@id='thread_title_*]");
But it's not working. Is there a solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有效(经过测试):
编辑:
在删除之前我查看了其他答案 - 我认为应该也有效(或者我是这么认为的):
经过一番研究,发现 HtmAgilityPack 中对属性的 XPath 支持不支持正则表达式,但您可以使用诸如
starts-with
、contains
、translate 等函数
、substring-before
和substring-after
。只是一个需要注意的问题。This works (tested):
Edit:
I looked at the other answer before it was removed - and in my opinion that should have worked as well (or so I thought):
After some research it turns out that the XPath support in HtmAgilityPack for attributes does not support regular expressions but you can use functions such as
starts-with
,contains
,translate
,substring-before
andsubstring-after
. Just a gotcha to be aware of.