通过 C# Windows 表单执行网络搜索

Perform web searches through C # Windows forms

我正在尝试实现网络搜索以在 Google 搜索中获取网站的标题。
我得到的代码在其他网站上运行良好,但使用 Google 我得到了重复的结果。

我试了又试,就是看不出哪里错了。

代码简化:

public partial class Form1 : Form
{
    WebBrowser navegador = new WebBrowser();

    private void Form1_Load(object sender, EventArgs e)
    {
        navegador.ScriptErrorsSuppressed = true;
        navegador.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.datos);
    }

    private void datos(object sender, EventArgs e)
    {
        try
        {
            foreach (HtmlElement etiqueta in navegador.Document.All)
            {
                if (etiqueta.GetAttribute("classname").Contains("LC20lb DKV0Md"))
                {
                    listBox1.Items.Add(etiqueta.InnerText); 
                }
            }
        }
        catch (Exception exception) {  }
    }

    private void function(object sender, EventArgs e)
    {
        /// string query = "https://google.com/search?q=" + query_box.Text;
        navegador.Navigate("https://google.com/search?q=water");
        /// this.Text = query;
    }
}

结果:

我不知道 google 是如何工作的,但你可以防止这样的重复

if(!listBox1.Items.Contains(etiqueta.InnerText))
        listBox1.Items.Add(etiqueta.InnerText);

经过几天研究和改进代码后,我决定将列表更改为 table。 此外,现在搜索没有重复,并且按预期位于 table

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace SourceDownloader
{

    public partial class Form1 : Form
    {
        strs valor = new strs();
        public Form1()
        {
            InitializeComponent();
        }


        WebBrowser navegador = new WebBrowser();
        private void Form1_Load(object sender, EventArgs e)
        {
            navegador.ScriptErrorsSuppressed = true;

            navegador.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.datos);

        }
        private void datos(object sender, EventArgs e)
        {
            try
            {

                foreach (HtmlElement etq in navegador.Document.All)
                {
                    if (etq.GetAttribute("classname").Contains("r")) /// LC20lb DKV0Md
                    {
                        foreach (HtmlElement a_et in etq.GetElementsByTagName("a"))
                        {
                            valor.link = a_et.GetAttribute("href");
                        }
                        foreach (HtmlElement t_et in etq.GetElementsByTagName("h3"))
                        {
                            valor.tit = t_et.InnerText;
                        }
                        bool exist = dataGridView1.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToString(row.Cells["link"].Value) == valor.link);
                        var s1 = valor.link;
                        bool b = s1.Contains("google.com");
                        bool a = s1.Contains("googleusercontent");
                        if (!exist /* && !b && !a*/)
                        {

                            dataGridView1.Rows.Insert(0, valor.tit, valor.link);

                        }
                    }
                    if (etq.GetAttribute("classname").Contains("G0iuSb"))
                    {
                        valor.next = etq.GetAttribute("href");
                    }
                }
                more_ops.Enabled = true;
            }
            catch (Exception)
            {

            }
        }

        private void function(object sender, EventArgs e)
        {
            string query = "https://google.com/search?q=" + query_box.Text;
            navegador.Navigate(query);
            this.Text = query;
        }

        private void more_ops_Click(object sender, EventArgs e)
        {
            string query = valor.next;
            navegador.Navigate(query);
            this.Text = query;

        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            try
            {
                var msg = dataGridView1.CurrentCell.Value;
                System.Diagnostics.Process.Start(msg.ToString());
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        private void Enter(object sender, KeyPressEventArgs e)
        {



            if (e.KeyChar == (char)Keys.Return)
            {
                string texto = query_box.Text;
                texto = texto.Replace("\n", "");
                query_box.Text = texto;

                string query = "https://google.com/search?q=" + query_box.Text;
                navegador.Navigate(query);
                this.Text = query;
            }

        }
    }

    /// global values

    class strs
    {
        private string _link = "N/A";
        private string _tit = "N/A";
        private string _next = "N/A";
        public string tit
        {
            get
            {
                return _tit;
            }
            set
            {
                _tit = value;
            }
        }
        public string link
        {
            get
            {
                return _link;
            }
            set
            {
                _link = value;
            }
        }
        public string next
        {
            get
            {
                return _next;
            }
            set
            {
                _next = value;
            }
        }


    }
}