所以我是python和请求的新手,我正在尝试使用python请求登录到这个站点,它一开始似乎工作正常,但是一旦我使用会话并移动到另一个页面我一直得到重定向回登录页面(在python脚本中)。
我在使用IE 11测试网站时发现了类似的问题,但该网站使用chrome或ff工作正常。因此,我提取了开发人员工具,并在IE 11和Chrom上进行了比较,但Cookie不同:
IE 11:Cookie:__ qca = P0-193635655-1454037632726; PHPSESSID = f4hmvt7cvfrfqg0seivmu34fv7
铬:饼干:__ QCA = P0-701638259-1453424539435; SMFCookie474 =α%3A4%3A%7BI%3A0%3BS%3A5%3A%2299842%22%3Bi%3A1%3BS%3A40%3A%22dc7db4be19f7f4fc53d2411065b8b7bf5705c83a%22%3Bi%3A2%3Bi%3A1454036767%3Bi%3A3%3Bi% 3A0%3B%7D; PHPSESSID = o43kja5a9iq1b8ibdll5sh2s24
Python请求:Cookie PHPSESSID = hotj0fpm5pbipktmm51kirul45
所以我假设我错过了一些使用python请求的cookie来使这项工作。有什么建议吗?
def login_test():
payload = {
'user': 'user',
'passwrd': 'pass'
}
with requests.Session() as s:
p = s.post('http://www.example.com/SMF/index.php?action=login2', data=payload)
# print the html returned or something more intelligent to see if it's a successful login page.
f = open("ouput.html", 'wb')
print (s.cookies)
#f.write(p.text.encode('utf8'))
print ("Status Code: ", p.status_code)
payload = {
'test': 'testparm'
}
r = s.get('http://www.example.com/cgi-bin/vote_rank.cgi', data=payload)
print (r.status_code)
print (s.cookies)
print (r.headers)
f.write(r.text.encode('utf8'))
f.close()
login_test()
答案 0 :(得分:0)
我能够通过手动添加cookie来解决这个问题:
def login_test():
payload = {
'user': 'user',
'passwrd': 'pass'
}
s = requests.Session()
p = s.post('http://www.example.com/SMF/index.php?action=login2', payload, allow_redirects=False)
# print the html returned or something more intelligent to see if it's a successful login page.
#f = open("ouput.html", 'wb')
cookies = p.headers['Set-Cookie']
n_cookies = {}
m = re.compile('(\w+)=([a-z%0-9A-Z]+)[^;]*')
for (name, content) in re.findall(m, cookies):
if name != "expires":
n_cookies[name] = content
r = s.get('http://www.example.com/cgi-bin/vote_rank.cgi?mud=test', cookies = n_cookies)
如果有人有更好的答案,请添加。