【问题标题】:link extraction using HtmlAgilityPack and c#使用 HtmlAgilityPack 和 c# 进行链接提取
【发布时间】:2016-05-31 04:01:55
【问题描述】:

我想提取谷歌结果链接
我的代码可以提取链接,但这些链接不是我期望提取的。 我的程序会提取“a href”标签内的链接,但搜索结果中的所有链接都不是适当的链接,广告链接,谷歌链接也包括在内 我该怎么办?

using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Search
{
public partial class Form1 : Form
{
    // load snippet
    HtmlAgilityPack.HtmlDocument htmlSnippet = new HtmlAgilityPack.HtmlDocument();

    public Form1()
    {
        InitializeComponent();
    }

    private void btn1_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        StringBuilder sb = new StringBuilder();
        byte[] ResultsBuffer = new byte[8192];
        string SearchResults = "http://google.com/search?q=" + txtKeyWords.Text.Trim();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SearchResults);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        Stream resStream = response.GetResponseStream();
        string tempString = null;
        int count = 0;
        do
        {
            count = resStream.Read(ResultsBuffer, 0, ResultsBuffer.Length);
            if (count != 0)
            {
                tempString = Encoding.ASCII.GetString(ResultsBuffer, 0, count);
                sb.Append(tempString);
            }
        }

        while (count > 0);
        string sbb = sb.ToString();

        HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
        html.OptionOutputAsXml = true;
        html.LoadHtml(sbb);
        HtmlNode doc = html.DocumentNode;

        foreach (HtmlNode link in doc.SelectNodes("//a[@href]"))
        {
            //HtmlAttribute att = link.Attributes["href"];
            string hrefValue = link.GetAttributeValue("href", string.Empty);
     //       if ()
            {
                int index = hrefValue.IndexOf("&");
                if (index > 0)
                {
                    hrefValue = hrefValue.Substring(0, index);
                    listBox1.Items.Add(hrefValue.Replace("/url?q=", ""));
                }
            }
        }
    }
}

}

如果我想使用“a href”标签,我必须在 If 中添加一些条件
但我不知道我应该在这里使用什么条件:

if ()

我在某个地方读到过关于提取 cite 标签而不是 ahref 标签的内容
有人可以帮忙吗?

【问题讨论】:

    标签: c# html search-engine html-agility-pack


    【解决方案1】:

    要获取包含在 cite 元素中的链接,只需访问它们的内部文本,例如:

        HtmlWeb w = new HtmlWeb();
        var hd = w.Load("http://www.google.com/search?q=veverke");
    
        var cites = hd.DocumentNode.SelectNodes("//cite");
    
        foreach (var cite in cites)
            Console.WriteLine(cite.InnerText);
    

    【讨论】:

    • @ververke Tnx 很多。如果我想像谷歌结果页面一样获取每个 url 的标题和 url 的描述,我该怎么办?
    • 我对@9​​87654323@不太熟悉,所以我会使用css来代替。为了能够将 css 选择器与 HtmlAgilityPack 一起使用,请安装 ScrapySharp nuget 包,添加 using ScrapySharp.Extensions,您现在将在 HtmlNode 对象中获得 CssSelect 方法。现在运行hd.DocumentNode.CssSelect(".rc h3").Select(n => n.InnerText) 应该可以了。
    • tnx 很多。 hd.DocumentNode.CssSelect(".rc h3").Select(n => n.InnerText) 用这一行我如何获得每个 URL 的标题?
    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    相关资源
    最近更新 更多