urllib,urllib2和request模拟登陆人人网比对
一.urllib+urllib2+cookielib版
# -*- coding:utf-8 -*-
import urllib2,urllib,cookielib
data={"email":"your email","password":"your password"}
post_data=urllib.urlencode(data)#將dict轉換成url參數
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
headers ={"User-agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"}
req=urllib2.Request("http://www.renren.com/PLogin.do",post_data,headers)
content=opener.open(req)
for index, cookie in enumerate(cj):#獲得index和values
? print index,cookie;
con=opener.open("http://www.renren.com/548056053/profile")
print con.geturl()
二.requests版
# -*- coding:utf-8 -*-
import requests
s=requests.Session()
headers ={"User-agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"}
logininfo={"email":"your email","password":"your password"}
r=s.post("http://www.renren.com/PLogin.do",data=logininfo,headers=headers)
r2=s.get("http://www.renren.com/548056053/profile")
print r2.url
相比之下request方便不少,其中的session功能就是為這種cookie保持的需求而生的,這種情況根本不需要手動顯式處理cookie。
轉載于:https://blog.51cto.com/7305072/1261717
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的urllib,urllib2和request模拟登陆人人网比对的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 向大家推荐几本数据库入门的书
- 下一篇: 很酷的js拖拽效果