Python mechanize login Facebook, use beautifulshoup get profile picture failed

2.4k Views Asked by At

I'm trying to keep login Facebook using Python mechanize library and get Zuck's profile picture url using BeautifulSoup. Here is my code:

import cookielib
import mechanize
from BeautifulSoup import BeautifulSoup

# Browser
br = mechanize.Browser()

# Enable cookie support for urllib2
cookiejar = cookielib.LWPCookieJar()
br.set_cookiejar(cookiejar)

# Broser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

#
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# authenticate
br.open('https://www.facebook.com/')
br.select_form(nr=0)
# these two come from the code you posted
# where you would normally put in your username and password
br['email'] = 'my_email_address'
br['pass'] = 'my_password'
res = br.submit()

print "Success!\n"

url = 'https://www.facebook.com/zuck'
soup = BeautifulSoup(br.open(url).read())
# print soup
print [x for x in soup.findAll('img', {'class': 'profilePic img'})]

However, the html that BeautifulSoup read is not Zuck's Facebook homepage source code. The html starts with

<html lang="zh-Hans" id="facebook" class="no_js">...

It contains no 'img' with class = 'profilePic img' I need. What's the problem? Thanks!

0

There are 0 best solutions below