0

I'm trying to realize web searches to get the title of websites on a Google search.
I got a code that works well on other sites, but using Google I got duplicated results.

I have tried and tried, but I can't see where is the mistake.

Code simplified:

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;
    }
}

Result:

Result

Jimi
  • 29,621
  • 8
  • 43
  • 61
Cicker
  • 19
  • 11
  • 1
    See the notes and the method shown [here](https://stackoverflow.com/a/53218064/7444103). A class object that stores the results and the use of `GetHashCode()`, specifically. – Jimi Mar 07 '20 at 14:29

2 Answers2

1

I don't know how google works but you can prevent duplicates like this

if(!listBox1.Items.Contains(etiqueta.InnerText))
        listBox1.Items.Add(etiqueta.InnerText);
Ehsan Kiani
  • 3,050
  • 1
  • 17
  • 19
0

After a couple of days researching and improving the code I decided to change the list for a table. In addition, now the searches are not duplicated and are positioned in the table as expected

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;
            }
        }


    }
}

Cicker
  • 19
  • 11