python模拟登录人人网

参考:

http://www.cnblogs.com/txw1958/archive/2012/03/12/2392067.html

http://www.cnblogs.com/chenyg32/archive/2013/06/04/3116317.html

http://docs.python.org/2/howto/urllib2.html

http://stackoverflow.com/questions/189555/how-to-use-python-to-login-to-a-webpage-and-retrieve-cookies-for-later-usage

 1 import urllib,urllib2,cookielib
 2 from BeautifulSoup import BeautifulSoup
 3 url='http://www.renren.com/SysHome.do'
 4 resp1=urllib2.urlopen(url)
 5 source=resp1.read()
 6 soup1=BeautifulSoup(source)
 7 user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
 8 headers = { 'User-Agent' : user_agent}
 9 params={'domain':'renren.com','origURL':'http://www.renren.com/indexcon','email':'*********@****.com','password':'******'}
10 params=urllib.urlencode(params)
11 params=params.encode('ISO-8859-1')
12 cj=cookielib.CookieJar()
13 opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
14 urllib2.install_opener(opener)
15 resp2=urllib2.Request('http://www.renren.com/PLogin.do',params,headers)
16 response = urllib2.urlopen(resp2)
17 # print opener.open(resp2)
18 the_page=response.read()
19 print the_page

 

posted @ 2014-02-08 10:38  小菜鸟_yang  阅读(466)  评论(0编辑  收藏  举报