用requests模拟登陆weibo.cn,分析整个流程如下,第一,登陆weibo.cn获得登陆界面,模拟表单提交,post成功(下面截图的蓝色部分),成功后有一个重定向,从返回的header里头location可以获得,但是我用requests.header没有找到location值,请问这个怎么处理,后面附上了代码,麻烦有大牛能提点一下
import requests
from bs4 import BeautifulSoup as bs
weiboUrl = 'http://weibo.cn/pub/'
loginUrl = bs(requests.get(weiboUrl).content).find("div",{"class":"ut"}).find("a")['href']
origInfo = bs(requests.get(loginUrl).content)
loginInfo = origInfo.find("form")['action']
loginpostUrl = 'http://login.weibo.cn/login/'+loginInfo
print loginpostUrl
headers = {
'Host': 'login.weibo.cn',
'User-Agent' : 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)',
'Referer' : 'http://login.weibo.cn/login/?ns=1&revalid=2&backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&vt=',
}
postData = {
'mobile': YourName,
origInfo.find("form").find("input",{"type":"password"})['name']: YourPsw,
'remember':'on',
'backURL':origInfo.find("form").find("input",{"name":"backURL"})['value'],
'backTitle': origInfo.find("form").find("input",{"name":"backTitle"})['value'],
'tryCount': origInfo.find("form").find("input",{"name":"tryCount"})['value'],
'vk': origInfo.find("form").find("input",{"name":"vk"})['value'],
'submit': origInfo.find("form").find("input",{"name":"submit"})['value'],
}
req = requests.post(loginpostUrl, data=postData, headers=headers)
print req.headers
req2 = requests.get('http://weibo.cn/?vt=4',cookies=req.cookies)
print req2.content