【问题标题】:get links from a google search in C#从 C# 中的谷歌搜索中获取链接
【发布时间】:2011-07-07 23:42:18
【问题描述】:

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

吉尔

【问题讨论】:

  • 我有一个向谷歌发送请求并解析返回响应的项目​​。我们必须每年多次重写解析模块,以跟随谷歌的标记变化。糟透了。虽然修复解析代码通常只需要几个小时。
  • @Snowbear,你在使用 HtmlAgility 包进行解析吗?
  • @Shiv,不,它是一种遗留部分,它仍然使用正则表达式。谢谢你提到这一点,下次我们重写那个噩梦时,我会调查一下。
  • @你能把你的发给我吗?
  • @snowbear,是的,我认为如果您使用 HtmlAgility 包并搜索链接,那么(在这种情况下)没关系,因为最终结果是您仍在寻找链接。当然,如果 Google 将其更改为每个结果显示多个链接,则您必须找到一种方法来区分给定结果项的一个链接和其他链接。

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


【解决方案1】:

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

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=", ""));
                    }
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    如果你要走这条路,你应该使用HtmlAgility pack 进行解析。但是,更好的方法是使用 Google 的 API。看到这个帖子i need to know which of my url is indexed on google

    关于使用 HtmlAgility 包的一些代码,我的博客上有一个帖子 Finding links on a Web page

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2021-08-23
      • 2013-08-17
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多