在 C# 中获取 Yahoo 结果页面的标题和 URL
Get the titles and URLs of Yahoo result page in c#
我想使用 htmlagility pack 获取 Yahoo 结果页面的标题和 URL
HtmlWeb w = new HtmlWeb();
string SearchResults = "https://en-maktoob.search.yahoo.com/search?p=" + query.querytxt;
var hd = w.Load(SearchResults);
var nodes = hd.DocumentNode.SelectNodes("//a[@cite and @href]");
if (nodes != null)
{
foreach (var node in nodes)
{
{
string Text = node.Attributes["title"].Value;
string Href = node.Attributes["href"].Value;
}
}
有效,但搜索结果中的所有 link 都不合适 links 如何省略广告 link、Yahoo links 等。
我想访问正确的 links
这个怎么样:
HtmlWeb w = new HtmlWeb();
string search = "https://en-maktoob.search.yahoo.com/search?p=veverke";
//ac-algo ac-21th lh-15
var hd = w.Load(search);
var titles = hd.DocumentNode.CssSelect(".title a").Select(n => n.InnerText);
var links = hd.DocumentNode.CssSelect(".fz-15px.fw-m.fc-12th.wr-bw.lh-15").Select(n => n.InnerText);
for (int i = 0; i < titles.Count() - 1; i++)
{
var title = titles.ElementAt(i);
string link = string.Empty;
if (links.Count() > i)
link = links.ElementAt(i);
Console.WriteLine("Title: {0}, Link: {1}", title, link);
}
请记住,我使用的是来自 nuget 包 ScrapySharp
的扩展方法 CssSelect
。像安装 HtmlAgilityPack
一样安装它,然后在代码顶部添加一条 using 语句,如 using ScrapySharp.Extensions;
,就可以开始了。 (我使用它是因为它更容易引用 css
选择器而不是 xpath
表达式...)
关于跳过广告,我注意到这些雅虎搜索结果中的广告只会出现在最后一条记录上?假设我是正确的,直接跳过最后一个。
这是我为 运行 上面的代码得到的输出:
我想使用 htmlagility pack 获取 Yahoo 结果页面的标题和 URL
HtmlWeb w = new HtmlWeb();
string SearchResults = "https://en-maktoob.search.yahoo.com/search?p=" + query.querytxt;
var hd = w.Load(SearchResults);
var nodes = hd.DocumentNode.SelectNodes("//a[@cite and @href]");
if (nodes != null)
{
foreach (var node in nodes)
{
{
string Text = node.Attributes["title"].Value;
string Href = node.Attributes["href"].Value;
}
}
有效,但搜索结果中的所有 link 都不合适 links 如何省略广告 link、Yahoo links 等。
我想访问正确的 links
这个怎么样:
HtmlWeb w = new HtmlWeb();
string search = "https://en-maktoob.search.yahoo.com/search?p=veverke";
//ac-algo ac-21th lh-15
var hd = w.Load(search);
var titles = hd.DocumentNode.CssSelect(".title a").Select(n => n.InnerText);
var links = hd.DocumentNode.CssSelect(".fz-15px.fw-m.fc-12th.wr-bw.lh-15").Select(n => n.InnerText);
for (int i = 0; i < titles.Count() - 1; i++)
{
var title = titles.ElementAt(i);
string link = string.Empty;
if (links.Count() > i)
link = links.ElementAt(i);
Console.WriteLine("Title: {0}, Link: {1}", title, link);
}
请记住,我使用的是来自 nuget 包 ScrapySharp
的扩展方法 CssSelect
。像安装 HtmlAgilityPack
一样安装它,然后在代码顶部添加一条 using 语句,如 using ScrapySharp.Extensions;
,就可以开始了。 (我使用它是因为它更容易引用 css
选择器而不是 xpath
表达式...)
关于跳过广告,我注意到这些雅虎搜索结果中的广告只会出现在最后一条记录上?假设我是正确的,直接跳过最后一个。
这是我为 运行 上面的代码得到的输出: