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

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

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

吉尔

最佳答案

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

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

关于c# - 从 C# 中的谷歌搜索获取链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5179947/

相关文章:

java - 如何在 Google AppEngine 中搜索小于或等于的数字字段?

c# - 在 C# 中创建 xml

python - 在 tensorflow.metrics 中,precision_at_k 和 precision_at_top_k 之间的区别?

开放简历 |筛选冲浪 |散列 |图片搜索

node.js - 用于 Node.js 的 Google 搜索 API 包装器

google-app-engine - 谷歌应用引擎( python ): Search API : String Search

c# - Caliburn Micro 用户控制数据上下文

c# - 多部分/表单数据请求——上传 pdf 导致生成空白文件

c# - 如何使用异步来提高 WinForms 性能?

python - 将两个 id 合并到一个新表中?