0

首先,对不起,我的 英语 很
糟糕代码

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

}

我可以将此代码用于所有搜索引擎吗?我更改了这些行,使其适用于其他搜索引擎

if (!hrefValue.ToString().ToUpper().Contains("YAHOO") && hrefValue.ToString().Contains("/url?q=") && hrefValue.ToString().ToUpper().Contains("HTTP://"))


string SearchResults = "http://yahoo.com/search?q=" + textBox1.Text.Trim();

但它不起作用

我的另一个问题是这段代码只返回第一个页面链接。如果我想返回 N 个第一个链接,我该怎么办?
有人可以帮忙吗?

4

1 回答 1

2

首先,您对这个主题有不止一个问题。请为每个问题写一个主题。

在 Yahoo 的情况下,“ http://yahoo.com/search?q= ”是无效的,如果你尝试http://yahoo.com/search?q=stackoverflow你不会得到结果页面。您必须找到每个搜索引擎的搜索 url。例如雅虎有: https://search.yahoo.com/search?p=

if (!hrefValue.ToString().ToUpper().Contains("YAHOO") && hrefValue.ToString().Contains("/url?q=") && hrefValue.ToString().ToUpper().Contains("HTTP://"))您还必须为每个搜索引擎修改它。例如,您只能获取 HTTP 值,但 HTTPS 会被丢弃。

分页

Google 使用&start=进行分页,通常每页返回 10 个结果。所以如果你输入 start=20,你会得到 20 到 30 https://www.google.es/search?q=stackoverflow&start=20

雅虎还每页返回 10 个结果并使用 por pagination &b=。b=1 是第一页,b=11 是第二页,以此类推。示例:https ://search.yahoo.com/search?p=stackoverflow&b=11

我希望这可以帮助你。

于 2016-05-16T10:20:45.740 回答