Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRequest using Mozilla Firefox

I need to have access at the HTML of a Facebook page, to extract from it some data. So, I need to create a WebRequest.

Example:

My code worked well for other sites, but for Facebook, I must be logged in to can access the HTML.

How can I use Firefox data for creating a WebRequest for Facebook page?

I tried this:

List<string> HTML_code = new List<string>();
WebRequest request = WebRequest.Create(URL);
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
    string line;
    while ((line = stream.ReadLine()) != null)
    {
        HTML_code.Add(line);
    }
}

...but the HTML resulted is the HTML of Facebook Home Page when I am not logged in.

like image 415
Ionică Bizău Avatar asked Feb 22 '26 12:02

Ionică Bizău


1 Answers

If what you are trying to is retrieve the number of likes from a Facebook page, you can use Facebook's Graph API service. Just too keep it simple, this is what I basically did in the code:

  1. Retrieve the Facebook page's data. In this case I used the Coke page's data since it was an example FB had listed.
  2. Parse the returned Json using Json.Net. There are other ways to do this, but this just keeps it simple, and you can get Json.Net over at Codeplex. The documentation that I looked for my code was from this page in the docs. Their documentation will also help you with parsing and serializing even more Json if you need to.

Then that basically translates in to this code. Just note that I left out all the fancy exception handling to keep it simple as using networking is not always reliable! Also don't forget to include the Json.Net library in your project!

Usings:

using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;

Code:

string url = "https://graph.facebook.com/cocacola";
WebClient client = new WebClient();
string jsonData = string.Empty;

// Load the Facebook page info
Console.WriteLine("Connecting to Facebook...");
using (Stream data = client.OpenRead(url))
{
    using (StreamReader reader = new StreamReader(data))
    {
        jsonData = reader.ReadToEnd();
    }
}

// Get number of likes from Json data
JObject jsonParsed = JObject.Parse(jsonData);
int likes = (int)jsonParsed.SelectToken("likes");

// Write out the result
Console.WriteLine("Number of Likes: " + likes);
like image 96
Josh Bowden Avatar answered Feb 25 '26 02:02

Josh Bowden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!