我需要能够从某个 url 获取页面主要内容。我需要做的一个很好的例子如下:http ://embed.ly/docs/explore/preview?url=http%3A%2F%2Fedition.cnn.com%2F2012%2F08%2F20%2Fworld% 2Fmeast%2Fflight-phobia-boy-long-way-home%2Findex.html%3Fiid%3Darticle_sidebar
我正在使用带有 C# 语言的 asp.net。
我需要能够从某个 url 获取页面主要内容。我需要做的一个很好的例子如下:http ://embed.ly/docs/explore/preview?url=http%3A%2F%2Fedition.cnn.com%2F2012%2F08%2F20%2Fworld% 2Fmeast%2Fflight-phobia-boy-long-way-home%2Findex.html%3Fiid%3Darticle_sidebar
我正在使用带有 C# 语言的 asp.net。
解析 html 页面并猜测主要内容并不是一个简单的过程。我建议使用NReadability和HtmlAgilityPack
这是一个如何完成的示例。在 NReadability 对页面进行转码后,正文始终div
带有 id 。readInner
string url = "http://.......";
var t = new NReadability.NReadabilityWebTranscoder();
bool b;
string page = t.Transcode(url, out b);
if (b)
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
var title = doc.DocumentNode.SelectSingleNode("//title").InnerText;
var text = doc.DocumentNode.SelectSingleNode("//div[@id='readInner']")
.InnerText;
}