search keywords in google through c# window applic

2019-02-11 01:13发布

问题:

i want to work on a scraper program which will search keyword in google. i have problem in starting my scraper program. my problem is: let suppose window application(c#) have 2 textboxes and a button control. first textbox have "www.google.com" and the 2nd textbox contain keywork for example:

textbox1: www.google.com textbox2: "cricket"

i want code to add to the button click event that will search cricket in google. if anyone have a programing idea in c# then plz help me.

best regards

回答1:

i have googled my problem and found solution to the above problem... we can use google API for this purpose...when we add reference to google api then we will add the following namespace in our program...........

using Google.API.Search;

write the following code in button click event

 var client = new GwebSearchClient("http://www.google.com");
        var results = client.Search("google api for .NET", 100);
        foreach (var webResult in results)
        {
            //Console.WriteLine("{0}, {1}, {2}", webResult.Title, webResult.Url, webResult.Content);
            listBox1.Items.Add(webResult.ToString ());
        }

test my solution and give comments .........thanx everybody



回答2:

I agree with Paqogomez that you don't appear to have put much work into this but I also understand that it can be hard to get started. Here is some sample code that should get you on the right path.

    private void button1_Click(object sender, EventArgs e)
    {
        string uriString = "http://www.google.com/search";
        string keywordString = "Test Keyword";

        WebClient webClient = new WebClient();

        NameValueCollection nameValueCollection = new NameValueCollection();
        nameValueCollection.Add("q", keywordString);

        webClient.QueryString.Add(nameValueCollection);
        textBox1.Text = webClient.DownloadString(uriString);
    }

This code will search for "Test Keyword" on Google and return the results as a string.

The problems with what you are asking is Google is going to return your result as HTML that you will need to parse. I really think you need to do some research on the Google API and what is needed to programmatically request data from Google. Start your search here Google Developers.

Hope this helps get you started on the right path.



标签: c# window