I'm trying to locate a websites position in google using c# and regex. I can match where the domain to check is a simple domain ie: 'mywebsite.com' but it doesn't work when the domain to be checked is 'mywebsite.com/a-product-name-p-23.html'
I assume this is the regex I am using but for the life of me I cannot work out what this should be. Basically I want to run the script to check my website pages position in serps but the url's could all be very different.
My code at the moment is:
public int GetPosition(Uri url, string searchTerm)
{
string raw = "http://www.google.co.uk/search?q={0}&num=100&hl=en&lr=&ie=UTF-8&safe=off&output=search#q={0}&hl=en&lr=&safe=off&prmd=imvns&ei=avgiT6HOGILPsgaRwfHpCA&start=0&sa=N&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=c7b3e04f0f892e66&biw=1366&bih=624";
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 = "(
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.AbsoluteUri))
return i + 1;
}
return 0;
}
An example of the data passed into 'string html' is:
BBC News - Home
If I use Keyword 'World News' and URL 'bbc.co.uk' then a serp position is retrieved but if I use the URL 'http://news.sky.com/home/world-news' OR 'www.msnbc.msn.com/id/3032507/' no serp position is retrieved despite both urls being on page1 of the serps.
I guess the regex cant match the more complex urls?
Any regex experts here who might be able to point me in the right direction or is there an easier way to do this in c#?
Many thanks, Sarah
If I use Keyword 'World News' and URL 'bbc.co.uk' then a serp position is retrieved but if I use the URL 'http://news.sky.com/home/world-news' OR 'www.msnbc.msn.com/id/3032507/' no serp position is retrieved despite both urls being on page1 of the serps.
I guess the regex cant match the more complex urls?
Any regex experts here who might be able to point me in the right direction or is there an easier way to do this in c#?
Many thanks, Sarah
Sam HobbsPosted Jan 30, 2012, 6:20 PM
Please do let me know if you need help with the code.
Sarah ReynoldsPosted Jan 30, 2012, 4:40 PM
Sarah ReynoldsPosted Jan 30, 2012, 4:38 PM
(not sure what happened with c&p before) I'll have another look at your example in more details. Like I said I couldnt actually get it to run earlier so I'll look into the errors and try and resolve them 1st. Thanks Sarah
Sam HobbsPosted Jan 30, 2012, 4:28 PM
Those web pages (at least the one I used in my sample for you) are complex; they are not simple pages. I am nearly certain that the only way you can get all the data that normally shows in the page is to navigate to it somehow. They create content dynamically. When you get the HTML from the web site, the scripts have not executed that generate additional HTML. You might be able to analyze the scripts to determine where the data is coming from and develop a more direct way to get the data but I really think the data is not in the HTML that exists in the server that you get with a HttpWebRequest.
Thank you for mentioning that the links you need have the attribute class="r". I will try adding that to my sample.
Sarah ReynoldsPosted Jan 30, 2012, 3:39 PM
Sam HobbsPosted Jan 30, 2012, 3:30 PM
What do you mean by "the console kept throwing mshtml.HTMLDocumentClass errors"?
I hope Vulpes understands what I am saying about the problem of downloading the HTML in the manner you are doing it.
Sarah ReynoldsPosted Jan 30, 2012, 2:25 PM
Sky News - World News: First for breaking global and international ...
I guess I need to look at this a bit differently as the results seen in the browser often differ. (ie:bbc news actually returns 5 results but in the browser they count for 1 position in the serps)
The regex definitely matches more urls on testing so I will definitely use that when I get round to this again so my thanks for posting and the time taken to help.
Sam, Unfortunately your project didn't work for me as the console kept throwing mshtml.HTMLDocumentClass errors but I could see enough to see this isn't how I would like to perform this.
Basically, the idea was to pass a keyword to google search and then inspect the results for the position of a given url. (All without opening a browser window) The original method with Vulpes' regex works to a point but does not take into account news/shopping results mixed into the page so the returned serps position is slightly different to the results viewed in a browser.
Thanks for your help guys.
Sarah
Sam HobbsPosted Jan 30, 2012, 2:12 AM
The attached project is a console application that shows all the links in the page. It writes the links to a file, because I am not sure what you are looking for. Note that when it executes it waits for a key to press. This is done for two reasons; to keep the window open, but also to give time for the document to completely download. The page downloads quickly, but not quickly enough for you to access the page immediately. You can look at the Links.txt file in the project's bin\Debug folder without building the project to see the links my program found.
I am not sure what you need to do but I hope this points you in a useful direction.
VulpesPosted Jan 29, 2012, 1:32 PM
Sam HobbsPosted Jan 28, 2012, 11:16 PM
I have used the DOM to parse HTML, such as in my Introduction to Web Site Scraping. The DOM is designed to do that. The mshtml component has the classes for using the DOM. Reead my article for a description of it.
I am not sure I understand what you are trying to do, but if I understand then you can use an InernetExplorer object in the shdocvw component to navigage to Google. You will want to add a handler for the DocumentComplete event. Then (in or after the DocumentComplete) you can use the Document property of the InernetExplorer object to access the HTML document (a mshtml.HTMLDocument object). I am not sure what you need to do from there, but note that there is a links property that is a collection of all the links in the page.
VulpesPosted Jan 28, 2012, 5:50 PM
However, the problem is clearly more complicated than that and, as I don't have much time just now, I'll try and have another look at it tomorrow when I can look at the problem as a whole.
Sarah ReynoldsPosted Jan 28, 2012, 12:22 PM
Thanks for that. Unfortunately its not returning any results for me (even the 2 examples in the OP) Did this actually return a result for you? Did you hardcode your keyword/url or pass them from a textbox?
Thanks
Sarah
VulpesPosted Jan 28, 2012, 8:45 AM