【问题标题】:Perform web searches through C # Windows forms通过 C# Windows 窗体执行 Web 搜索
【发布时间】:2020-03-07 09:43:56
【问题描述】:

我正在尝试实现网络搜索以在 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;
    }
}

结果:

【问题讨论】:

  • 参见here 显示的注释和方法。一个类对象,存储结果和GetHashCode()的使用,具体来说。

标签: c# winforms webbrowser-control


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    经过几天的研究和改进代码,我决定更改表格的列表。 此外,现在搜索不会重复,并且按预期定位在表格中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-25
      • 2010-10-26
      • 1970-01-01
      • 2014-11-21
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多