python 用requests模組自動登入
用requests模組重新寫了下。
需要啟用requests的會話保持。要不然登入後,訪問其他網頁會斷掉連線
-- coding: utf-8 --
import urllib2
import urllib
import cookielib
import re
import sys
reload(sys)
import requests
from bs4 import BeautifulSoup
sys.setdefaultencoding(“utf-8”)
設定cookie
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
新增headers
opener.addheaders=[(“User-Agent”,”Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36”),]
urllib2.install_opener(opener)
登入網站
POST 資料
values = {
‘username’:’cqmyg12342322’,
‘password’:’cq123456’,
‘k’:’Fri Oct 23 2015 23:39:12 GMT+0800 (中國標準時間)40000’
}
post_header ={
“Referer”:”
“User-Agent”:”Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36”,
“Accept-Encoding”:”gzip, deflate”,
“Accept-Language”:”zh-CN,zh;q=0.8”,
“Connection”:”keep-alive”,
“Content-Length”:”175”,
“Content-Type”:”application/x-www-form-urlencoded”
}
儲存cookie
cj.save(‘H:\python_learn/wangyi.txt’)
設定會話話保持。用這個物件訪問網站
s=requests.Session()
r = s.post(url, data=values,headers=post_header)
print r.content
mm = s.get(‘http://www.xinxianwang.com/my/‘,timeout=20).content
soup = BeautifulSoup(mm)
print soup.title