从C#中的谷歌搜索获取链接

时间:2011-03-03 11:10:25

标签: c# information-retrieval google-search-api

我正在尝试通过C#在google中编写一个简单的搜索,它会运行我选择的查询并检索前50个链接。在彻底搜索了类似的工具\正确的API之后,我意识到它们中的大多数已经过时了。我的第一次尝试是创建一个“简单的HttpWebRequest”并扫描收到的WebResponse以获取“href =”,结果证明它根本没有回报(冗余)并且非常令人沮丧。我确实有一个Google API,但我不确定如何将它用于此目的,虽然我知道每天有1000个限制。

吉尔

2 个答案:

答案 0 :(得分:6)

这是工作代码..显然你必须添加正确的表格和一些简单的控件......

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 (!hrefValue.ToString().ToUpper().Contains("GOOGLE") && hrefValue.ToString().Contains("/url?q=") && hrefValue.ToString().ToUpper().Contains("HTTP://"))
                {
                    int index = hrefValue.IndexOf("&");
                    if (index > 0)
                    {
                        hrefValue = hrefValue.Substring(0, index);
                        listBox1.Items.Add(hrefValue.Replace("/url?q=", ""));
                    }
                }
            }
        }
    }
}

答案 1 :(得分:1)

如果您要使用此路线,则应使用HtmlAgility包进行解析。但是,更好的方法是使用Google的API。请参阅此帖子i need to know which of my url is indexed on google

至于使用HtmlAgility包的一些代码,我的博客上有一篇文章 Finding links on a Web page

相关问题