【发布时间】:2012-01-05 08:11:46
【问题描述】:
我正在设计一个程序来抓取网页 thenextweb.com 的帖子(链接、帖子内容、图片、日期、作者等)
其一篇文章的 html 如下:
<div class="media-data">
<h4><a href="http://thenextweb.com/mobile/2012/01/05/nokia-reportedly-to-appoint-f-secure-founder-risto-siilasmaa-as-new-chairman/">Nokia to Name Risto Siilasmaa as New Chairman</a></h4>
<p class="article-meta"><a href="http://thenextweb.com/mobile/">TNW Mobile</a> • <a href="http://thenextweb.com/author/matt/" title="Posts by Matt Brian" rel="author">Matt Brian</a> • <span class="date" title="1325748846">January 5, 2012</span></a></p>
<p>Nokia is reportedly planning to nominate and name Risto Siilasmaa, founder of Finnish anti-virus and computer security F-Secure, as its new chairman by the end of the month, Finland’s Helsingin Sanomat reports…</p>
</div>
这是主页上接下来 15 个帖子的 html。 为了访问我使用过的内容:
var webGet = new HtmlWeb();
var document = webGet.Load(url);
var infos = from info in document.DocumentNode.SelectNodes("//div[@class ='media-data']//h4//a")
select new
{
LinkURL = info.Attributes["href"].Value,
Text = info.InnerText
};
lvLinks.DataSource = infos;
lvLinks.DataBind();
并访问我使用的作者、日期等信息:
var infos = from info in document.DocumentNode.SelectNodes("//div[@class ='media-data']//p[@rel = 'author']")
select new
{
Author = info.InnerText
};
lvLinks.DataSource = infos;
lvLinks.DataBind();
我使用列表视图控件将 ASP 页面上的数据显示为<li> <%# Eval("Text") %> - <%# Eval("LinkUrl") %> </li>
但我想要一种方法,以便我可以一次性访问所有这些...无需为 链接、内容 和其他 作者、日期 编写不同的代码强>等
有没有一种方法可以让我在<div class="media-data">... </div>tags 下为我想要的任何节点写入和检索信息并存储它?
请提出这个建议,因为附上作者、日期信息以及帖子链接本身非常重要。我做不到。
谢谢
【问题讨论】:
-
有什么理由不使用提供的所有故事/热门故事 RSS 提要?
-
我需要对所有故事进行一些自定义...这就是为什么我只需要从网页中获取它们。
标签: c# asp.net html web-crawler html-agility-pack