Suryakavitha
Suryakavitha

Reputation: 1411

keyword position in google search using C#

I am using a website to find a keyword position in google search ... i have two text boxes(one textbox for getting URL and another textbox for getting Keyword) and ,one label(to display the position of the keyword in the URl) and one button in my page... When i clicked that button it have to get the URL and Keyword and search the keyword in google and give the position.... i got that URL and Keyword and it s searching in the google properly but i have not get that Position...

How shall i get that position? Anyone Tell me the solutionof this... My code is

 protected void btnSubmit_Click(object sender, EventArgs e)
 {
    string strUrl = txtUrl.Text;
    Uri newUri = new Uri("http://www.Yahoo.com");
    int I = GetPosition(newUri, txtKeyword.Text);
 }


public static int GetPosition(Uri url, string searchTerm)
{
    string raw = "http://www.google.com/search?num=39&q={0}&btnG=Search";
    string search = string.Format(raw, HttpUtility.UrlEncode(searchTerm));

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream(),    Encoding.ASCII))
        {
            string html = reader.ReadToEnd();
            return FindPosition(html, url);
        }
    }
}


private static int FindPosition(string html, Uri url)
{
    string lookup = "(<a class=l href=\")(\\w+[a-zA-Z0-9.-?=/]*)";
    MatchCollection matches = Regex.Matches(html, lookup);

    for (int i = 0; i < matches.Count; i++)
    {
        string match = matches[i].Groups[2].Value;
        if (match.Contains(url.Host))
            return i + 1;
    }

    return 0;
}

Here i get that matches.count always 0 so it always returns Zero... How can i get matches.count

Upvotes: 0

Views: 4178

Answers (1)

Jeff Mattfield
Jeff Mattfield

Reputation:

In the HTML for a Google results list, the href attribute for each result link appears before the class attribute, not after it.

Try this:

string lookup = "(<a href=\")(\\w+[a-zA-Z0-9.-?=/]*)\" class=l";

Upvotes: 2

Related Questions