python 请求重定向
python requests with redirection
正在尝试在 http://72.ru site, noticed that there were a redirect to https://loginka.ru/auth/ 上进行身份验证。发现有 302 POST 具有数据形式的普通凭据。从 Chrome 复制 headers 可以在 cURL 中重现,但仍然无法在请求模块中访问。
警告:页面全是俄语字母,请在框中注册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)
为什么无法验证,我做错了什么?
我认为你需要指定 allow_redirects=True
s.post('http://72.ru/passport/login.php', allow_redirects=True)
from calendar import timegm
from time import gmtime
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)
epoch1 = '%s000' % timegm(gmtime())
s.get('http://72.ru/')
epoch2 = '%s000' % timegm(gmtime())
login_url = 'https://loginka.ru/service/api/passport/auth/token/?callback=jQuery17107978048569057137_%s&_=%s' % (epoch1, epoch2)
s.get(login_url)
epoch3 = '%s000' % timegm(gmtime())
params = {
'callback': 'jQuery17107978048569057137_%s' % epoch1,
'email': username, # Username Email
'password': password, # Password
'remember': 0,
'_': epoch3
}
r = self.s.get('https://loginka.ru/service/api/passport/auth/login/', params=params)
print r.content
登录此网站有一种更简单的方法。
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)
正在尝试在 http://72.ru site, noticed that there were a redirect to https://loginka.ru/auth/ 上进行身份验证。发现有 302 POST 具有数据形式的普通凭据。从 Chrome 复制 headers 可以在 cURL 中重现,但仍然无法在请求模块中访问。
警告:页面全是俄语字母,请在框中注册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)
为什么无法验证,我做错了什么?
我认为你需要指定 allow_redirects=True
s.post('http://72.ru/passport/login.php', allow_redirects=True)
from calendar import timegm
from time import gmtime
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)
epoch1 = '%s000' % timegm(gmtime())
s.get('http://72.ru/')
epoch2 = '%s000' % timegm(gmtime())
login_url = 'https://loginka.ru/service/api/passport/auth/token/?callback=jQuery17107978048569057137_%s&_=%s' % (epoch1, epoch2)
s.get(login_url)
epoch3 = '%s000' % timegm(gmtime())
params = {
'callback': 'jQuery17107978048569057137_%s' % epoch1,
'email': username, # Username Email
'password': password, # Password
'remember': 0,
'_': epoch3
}
r = self.s.get('https://loginka.ru/service/api/passport/auth/login/', params=params)
print r.content
登录此网站有一种更简单的方法。
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)