【问题标题】:How to parse URL from anchor tag如何从锚标记解析 URL
【发布时间】:2015-08-28 15:25:57
【问题描述】:

我想检索HTML 文档中<a> 标记中的URL。这是标签:

<a href="index.php?option=com_remository&amp;Itemid=43&amp;func=fileinfo&amp;id=49"><img src="http://dziekanat.wzim.sggw.pl/components/com_remository/images/file_icons/New.gif" width="16" height="16" border="0" align="middle" alt="file_icons/New.gif"/><b>&nbsp;Plan STAC lato 2014_15</b></a>

解析后我应该得到

index.php?option=com_remository&amp;amp;Itemid=43&amp;amp;func=fileinfo&amp;amp;id=49

我应该使用什么正则表达式模式?

我想用正则表达式来做这件事,因为 HTML 文档本身已经很老了,而且没有任何 ID 可供参考。因此,我无法使用任何更复杂的工具(如Html Agility Pack)来做到这一点。

整个文档可以在这里找到:http://dziekanat.wzim.sggw.pl/index.php?option=com_remository&Itemid=43&func=select&id=2

【问题讨论】:

  • 你是如何找到这个特定的&lt;a&gt;的?

标签: c# html regex


【解决方案1】:

因此,我无法使用任何更复杂的工具(如Html Agility Pack)来做到这一点。

为什么不呢?这对我有用

var html = new Webclient().DownloadString("http://dziekanat.wzim.sggw.pl/index.php?option=com_remository&Itemid=43&func=select&id=2");
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);


var links = doc.DocumentNode.Descendants("a")
            .Select(a => a.Attributes["href"].Value)
            .ToList();

此 Xpath 返回您的链接

var link = doc.DocumentNode.SelectSingleNode("//table[@class='sectiontableentry1']//a")
            .Attributes["href"].Value;

【讨论】:

  • 谢谢!这正是我想要的。
【解决方案2】:

给你:

string Pattern = @"<a[^>]*?href\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";

【讨论】:

    猜你喜欢
    • 2013-12-10
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 2019-06-03
    • 2016-01-30
    相关资源
    最近更新 更多