python模拟登陆遇到重定向_利用python的requests模拟登陆weibo.cn碰到302如何解决

用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值