【问题标题】:Extract all a `href`s from webpage with htmlagilitypack/requests anything使用 htmlagilitypack/requests 从网页中提取所有“href”
【发布时间】:2018-01-26 06:18:42
【问题描述】:

我有这个网页来源:

<a href="/StefaniStoikova"><img alt="" class="head" id="face_6306494" src="http://img0.ask.fm/assets/054/771/271/thumb_tiny/sam_7082.jpg" /></a>
<a href="/devos"><img alt="" class="head" id="face_18603180" src="http://img7.ask.fm/assets/043/424/871/thumb_tiny/devos.jpg" /></a>
<a href="/frenop"><img alt="" class="head" id="face_4953081" src="http://img1.ask.fm/assets/029/163/760/thumb_tiny/dsci0744.jpg" /></a>

我想在&lt;a href-" 之后提取字符串。但我的主要问题是这些字符串是不同的,我似乎没有找到办法。既没有 agilitypack 也没有 webrequests。

也许有人对正则表达式有想法?分享吧。

【问题讨论】:

  • 您要提取href 属性还是整行?如果你想在客户端使用 jquery 很容易。

标签: c# httpwebrequest html-agility-pack


【解决方案1】:

使用 HtmlAgilityPack 获得所需的内容应该非常简单。假设您已将文档加载到名为 docHtmlDocument 对象中:

HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("//a[@href]");

foreach (HtmlNode node in collection)
{
    // Do what you want with the href value in here. As an example, this just
    //  just prints the value to the console.
    Console.WriteLine(node.GetAttributeValue("href", "default"));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2021-09-07
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多