Python mechanize login Facebook, use beautifulshoup get profile picture failed

不羁岁月 提交于 2019-12-24 03:53:32

问题


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!

来源:https://stackoverflow.com/questions/30898033/python-mechanize-login-facebook-use-beautifulshoup-get-profile-picture-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!