python重定向响应信息头,带有重定向的python请求

Trying to authenticate on http://72.ru site, noticed that there were a redirect to https://loginka.ru/auth/. Found that there were 302 POST with plain credentials in data form. Copying headers from Chrome can reproduce that in cURL, but still can't reach in requests module.

Warning: page is full of russian letters, registration in the box north-east

with requests.Session() as s:

s.auth = ('EMAIL', 'PASSWD')

s.post('http://72.ru/passport/login.php')

p = s.get('http://72.ru/job/favorite/vacancy/')

# will print True if logged

print('some title from favorite page, if logged' in p.text)

Why can't authenticate, what am I doing wrong?

解决方案

There is a much simpler way to perform login to this website.

import requests

headers = {

"User-Agent":

"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36",

}

s = requests.session()

s.headers.update(headers)

# There is a dedicated login page, which is the url of the Login button on the site, you can open that directly.

# Requests will automatically take care of rediects

s.get('https://loginka.ru/auth/?url=http%3A%2F%2F72.ru')

# Generate the post data

data = {

'url': 'http://72.ru',

'email': username,

'password': password

}

# Perform the post request

r = s.post('https://loginka.ru/auth/?url=http%3A%2F%2F72.ru', data=data)

# There is an extra post request on this site which uses token from redirect url

token = r.url[r.url.index('token=')+6:]

url = 'http://72.ru/put_token_to_user/?token=' + token + '&dummy_put_token_to_user=yes'

headers2 = {'X-Requested-With': 'XMLHttpRequest', 'Referer': r.url}

r = s.get(url, headers=headers2)

r = s.get('http://72.ru/passport/mypage.php')

print r.url

print r.status_code

with open('abc.txt', 'wb') as f:

f.write(r.content)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值