【问题标题】:radio button filters in wpf c# windows applicationwpf c# windows应用程序中的单选按钮过滤器
【发布时间】:2016-02-14 01:42:50
【问题描述】:

我正在开发一个链接提取器 c# 应用程序。根据关键字抓取网址。就像当用户使用 txtKeyWords 文本框字段搜索关键字并点击搜索按钮时,他将能够使用开放的谷歌搜索运算符 "http://www.google.com/search?num=1000&q=" 在任何关键字上获取所需的网址。
现在我想知道如何添加此代码
http://www.google.com/search?num=50&q=allinurl:site:.edu 以便仅获取 rb1.Checked 下的 edu 链接。就像当用户检查无线电 btn1 并在 txtKeyWords 文本框字段中输入所需的关键字时,他能够获取与查询相关的 .edu 链接。我已经做了几次尝试,但无法这样做。这是Search Button的代码

listBox1.Items.Clear();
            StringBuilder sb = new StringBuilder();
            byte[] ResultsBuffer = new byte[8192];
            string SearchResults = "http://www.google.com/search?num=1000&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=", ""));
                    }
                }
            }

【问题讨论】:

    标签: c# .net wpf radio-button


    【解决方案1】:

    如果您的搜索始终使用 Google,您可能只想使用 URL 本身进行过滤。

    因此,您可以将单选按钮文本附加到类似于此的字符串...

    字符串 SearchResults = "http://www.google.com/search?num=1000&q=" + txtKeyWords.Text.Trim() + "&as_sitesearch=" + radioButton1.Text.Trim();

    这会产生一个这样的 URL...

    https://www.google.com/search?num=1000&q=cars&as_sitesearch=.edu

    然后应通过您的单选框选择(在本例中为 .edu)过滤结果。

    现在每个结果应该包含“.edu”

    希望这会有所帮助!

    忘了提一下:如果您需要进一步过滤,请使用 Google 高级搜索,它会构建 URL 供您使用... https://www.google.com/advanced_search

    【讨论】:

    • @ForulaChris 仍然出现问题。我知道您提供了一个很好的解决方案,但我的问题是假设这是搜索 Button 代码 string SearchResults = "http://www.google.com/search?num=1000&q=" + txtKeyWords.Text.Trim() 当我将字符串值 .edu 分配给单选按钮 1 并将其与 search button 代码连接时,它会向我显示错误的结果.我的意思是它不返回.edu 结果。它选择作为关键字而不是搜索运算符..任何解决方案.??
    • 谢谢,运行良好。
    猜你喜欢
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    相关资源
    最近更新 更多