Now we've been able to get a nice looking URL, which is actually a URL to our Facebook wall and now
we just need to make another request so we can get to our Facebook wall.
So let's try and do that.
So const.
Home page.
Await request.
Obviously request get because it's a get request we're not posting now and headers location.
And then let's try and see if we can write, because this is supposed to be HTML.
I mean, we're supposed to get a body element inside of this request, which is pure HTML that we can
save much like we got inside of our postman.
We have this.
This large body with lots of HTML, which is basically our Facebook wall page, and we need to be getting
the same thing inside of our request here.
Now let's try and write a function to save this HTML to a file because that's pretty handy to have.
So we can look on the file and load it into our browser to see how the response is looking.
So function write file, we take in a body and then we say FS.
That's import fs.
Const FS require FS and FS is already in NodeJS by default, so you don't need to use NPM.
FS write file.
And then we put in the path of the file.
So we'll just write tested HTML.
Then we have the body, then a callback, if there's any errors or if it's successful.
So if there's an error, we say console error.
Ever.
And if not, we'll just write.
HTML was saved.
And then we can use the function over here.
Right.
File home page.
Now let's try and run it again and look at what result we're getting from Facebook.
So HTML was saved.
And we can see there's actually this HTML page.
It's a little hard to read when it's only one line, but we can actually open it inside of our browser.
So if I go inside the folder for our project, right click the text HTML, then open it in Chrome.
We can see the page that we actually got from Facebook.
Now, here it says in Danish, in my local language, that I need to log on first.
So that means something with the log on went wrong when we tried to make this request.
And why did something go wrong?
What went wrong?
Well, when you log on to the Facebook page using this post request, Facebook is setting some cookies
on your browser or in this case, in Postman.
And that's how Facebook knows that you logged in successfully.
And since we're doing this request without saving or reading any cookies, Facebook doesn't know we're
logged in.
So how do we also use cookies inside of request?
Well, actually, it's really simple.
We just go inside of our default options and we set char to true.
So they call it char, just like a just like a cookie jar.
So if you set the char to true, then request is automatically going to save any cookies that it's going
to get from Facebook.
And the next request you make using request is going to have those cookies.
So then Facebook can see that you've successfully logged in.
Now let's save that and let's try and log in again.
And there we go.
Now let's try and reload the page.
I have it opened here.
This is the local file.
Reloaded it and there we go.
We can see that I've been successfully logged in now.
And this is just the page that we saved from from Facebook and yeah, that's it.
Now we can also try and do some selecting and basically we can do whatever we want.
You can scrape this file as much as you like to.
You can select these elements inside of Chario.
Notice that all of the Oedipus seems to have a roll of article attributes, so you can select all of
the elements with a roll of article with Cheerio.
And you can even load the file just this HTML file, load it using FS and then load it using cheerio
and then you can select elements and extract all of the data you want.
So that's how you can scrape Facebook using just request and cheerio.