【问题标题】:How to Get all the websites related to the keyword in Windows Form C#如何在Windows Form C#中获取与关键字相关的所有网站
【发布时间】:2016-06-08 17:07:38
【问题描述】:

这是我的过程: 我有一个textbox,用户会在其中输入关键字,例如games,然后输入后所有与游戏相关的网站都会以windows形式输出。

基本上我尝试使用 Google Search API,使用以下代码:

const string apiKey = "";
const string searchEngineId = "";
const string query = "games";
CustomsearchService customSearchService = new CustomsearchService(new Google.Apis.Services.BaseClientService.Initializer() { ApiKey = apiKey });
Google.Apis.Customsearch.v1.CseResource.ListRequest listRequest = customSearchService.Cse.List(query);
listRequest.Cx = searchEngineId; 
Search search = listRequest.Execute();
foreach (var item in search.Items)
{
    Console.WriteLine("Title : " + item.Title + Environment.NewLine + "Link : " + item.Link + Environment.NewLine + Environment.NewLine);
}

但我的问题是 100 个查询/天和 10 个结果/查询的限制不适用。

所以我决定使用 HttpWebRequest 和 HttpWebResponse 的方法, 这是我从网上看到的代码:

StringBuilder sb = new StringBuilder();

// used on each read operation
byte[] buf = new byte[8192];
string GS = "http://google.com/search?q=sample";
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GS);

// execute the request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
    // fill the buffer with data
    count = resStream.Read(buf, 0, buf.Length);
    // make sure we read some data
    if (count != 0)
    {
        // translate from bytes to ASCII text
        tempString = Encoding.ASCII.GetString(buf, 0, count);

        // continue building the string
        sb.Append(tempString);
    }
}
while (count > 0);

我的问题是它返回整个 HTML,是否可以像使用 Google Search API 一样只获取 URL

【问题讨论】:

    标签: c# winforms parsing


    【解决方案1】:

    这就是它的工作方式,您要么必须为 API 付费,要么解析 HTML - 后者的合法性值得怀疑。

    【讨论】:

    • 嗯,我想我别无选择,只能解析
    • 这不是谷歌希望你做的事情——所以不要指望它容易或可靠。祝你好运。
    【解决方案2】:

    使用带有 css 选择器的 html 解析器,工作量并不大(解决方案基于此 java 教程:http://mph-web.de/web-scraping-with-java-top-10-google-search-results/)。我使用 Dcsoup(https://github.com/matarillo/dcsoup不完整的 Jsoup 端口)作为示例,因为我习惯了 Jsoup(https://jsoup.org/apidocs/),但可能还有其他更好维护的 c# html 解析器等。

    // query results on page 14, to demonstrate that limit of results is avoided
    int resultPage = 130;
    string keyword = "test";
    string searchUrl = "http://www.google.com/search?q="+keyword+"&start="+resultPage;
    
    System.Net.WebClient webClient = new System.Net.WebClient();
    string htmlResult = webClient.DownloadString(searchUrl);
    
    Supremes.Nodes.Document doc = Supremes.Dcsoup.Parse(htmlResult, "http://www.google.com/");
    
    // parse with css selector
    foreach (Supremes.Nodes.Element result in doc.Select("h3.r a")) 
    {
        string title = result.Text;
        string url = result.Attr("href");
    
        // do something useful with the search result
        System.Diagnostics.Debug.WriteLine(title + " -> " + url);
    }
    

    所需的选择器h3.r a 可能会改变。一个更稳定的替代方法可能是解析所有元素并检索具有 href 属性的元素,或者至少具有内置检查(检查具有大量结果的搜索词并解析,如果您的选择器没有结果,请发送给您通知,修复选择器)。

    另请参阅有关获取确切搜索词结果的答案:https://stackoverflow.com/a/37268746/1661938

    【讨论】:

      猜你喜欢
      • 2013-12-04
      • 2012-06-08
      • 1970-01-01
      • 2018-06-11
      • 2014-05-08
      • 1970-01-01
      • 2015-01-15
      • 2015-09-30
      • 2018-11-29
      相关资源
      最近更新 更多