Google has added a nice feature which makes you get instant info about any of famous people, for example when you search for "Barack Obama" you get a bio and a photo on the results page so you may not have to visit any of the results to get that info.
Live sample : http://goo.gl/vf1ti3
What I'm trying to do is to get the URL of the image at the left-side of instant info box. I want to accomplish that using System.Text.RegularExpressions.Regex
from the HTML code.
I can get the source of the result page with this code :
private void getInfoAboutCelebrities()
{
try
{
string celebrityName = null;
Dispatcher.Invoke((Action)delegate()
{
DisableUI();
celebrityName = celebrityName_textBox.Text;
});
celebrityName = HttpUtility.UrlEncode(celebrityName);
string queryURL = "http://www.google.com/search?q=" + celebrityName + "+Height&safe=active&oq=" + celebrityName + "+Height&gs_l=heirloom-serp.12...0.0.0.3140.0.0.0.0.0.0.0.0..0.0....0...1ac..24.heirloom-serp..0.0.0.hXJwfydNFhk";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(queryURL);
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0";
request.Method = "GET";
// make request for web page
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader htmlSource = new StreamReader(response.GetResponseStream());
string htmlStringSource = string.Empty;
htmlStringSource = htmlSource.ReadToEnd();
response.Close();
// Extracting height
var regex = new Regex(@"<span class=""kno-a-v"">(.*?)</span>");
var match = regex.Match(htmlStringSource);
var result = match.Groups[1].Value;
///////////////////////////////////////////////////////////
// Extracting photo ( which I couldn't do it
regex = new Regex(@"data:image/jpeg;base64(.*?)\x3d\x3d");
match = regex.Match(htmlStringSource);
///////////////////////////////////////////////////////////
result = HttpUtility.HtmlDecode(result);
if (String.IsNullOrWhiteSpace(result))
MessageBox.Show("Sorry, no such entry.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
else
{
Dispatcher.Invoke((Action)delegate()
{
preloader_Image.Visibility = Visibility.Hidden;
MessageBox.Show(result);
});
}
Dispatcher.Invoke((Action)EnableUI);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
Can anyone tell me what Regular Expression I should use? ( Because actually I can't even get the URL myself with viewing the source code! )