Shah
Shah

Reputation: 15

radio button filters in wpf c# windows application

I am working on an Link extractor c# application. that scrape urls according to keyword. Like when a user search the keyword using txtKeyWords text box field and hit the search button then he will be able to grab desired Urls on any keyword with open google search operator "http://www.google.com/search?num=1000&q=".
Now I am wondering how to add this code
http://www.google.com/search?num=50&q=allinurl:site:.edu for the sake of grabing only edu links under rb1.Checked. Like when user check radio btn1 and enter desired keyword in txtKeyWords text box field, then he able to grab query related .edu Links. I have done several tries but unable to do so. Here is the code of 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=", ""));
                    }
                }
            }

Upvotes: 1

Views: 185

Answers (1)

FormulaChris
FormulaChris

Reputation: 101

If your searches will always use Google, you may want to just use the URL itself to filter.

So you would append your radio button text to the string similar to this...

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

which would yield a URL such as this...

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

The results should then be filtered by your radio box selection (in this case, .edu).

Now every result should contain ".edu"

Hope this helps!

Forgot to mention: If you need to further filter, use the Google Advanced Search and it will build the URL for you to use... https://www.google.com/advanced_search

Upvotes: 2

Related Questions