This look a bit harder than usual. I'm a new user wget and i was thinking that maybe it's possible to somehow login into facebook and download only images from a specific conversation.

I mean, i'd like to tell wget my password and user or use cookies (idk how to write the line command either) and download ONLY IMAGES from a conversation.

I know that this is for sure possible but how would i do it?


You can script logging in (if you've got the credentials and if you're not facing things like Captchas) by using tools like Ruby Mechanize, but for starters, you could start by loging in with your web browser and exporting the cookies created in the process. For that, you don't need to know the internals of FB's login forms, but you do need to install a browser extension that allows you to export cookies (for a particular site or all of them).

With Chrom(e|ium) you can use https://chrome.google.com/webstore/detail/cookietxt-export/lopabhfecdfhgogdbojmaicoicjekelh . This exports cookies for the site you're currently on.

With your login cookies exported (I assume you saved them into cookies.txt) you need to load them into wget and supply a UserAgent string of a real browser (lest FB not give you what you want). Bellow I use "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36" which I got from http://useragentstring.com/ . Now you can wget a FB page as if you were on a web browser where you're logged into FB:

wget --load-cookies="cookies.txt"\
     -U "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36"\

Replace https://www.facebook.com with the URL of your conversation and you're halfway there.

The tougher part is dealing with the result. You can't exactly use wget features such as -A .jpg because a FB isn't really as simple static page. Most stuff on FB gets generated dynamically with javascript. If you don't want to use a full javascript engine and have it give you the images that get actually rendered, you could resort to crude methods such as grepping out all img tags (regardless of where they are and whether they're in effect) and then download each one of those:

cat "$YOUR_DOWNLOADED_HTML" | grep '<img[^>]*>' -o | grep -Po '(?<=src=\")[^"]*' | \
while read url
    wget --load-cookies="cookies.txt"\
         -U "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)            Chrome/37.0.2049.0 Safari/537.36"\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.