无法使用请求模块登录网站(Python版本3.5.1) [英] Cannot login to website using requests module (Python version 3.5.1)

查看:267
本文介绍了无法使用请求模块登录网站(Python版本3.5.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问网站以抓取某些信息,但是我无法通过Python发布登录信息。到目前为止,这是我的代码:

I am trying to access a website to scrape some information, however I am having trouble posting login information through Python. Here is my code so far:

import requests

c = requests.Session()
url = 'https://subscriber.hoovers.com/H/login/login.html'
USERNAME = 'user'
PASSWORD = 'pass'

c.get(url)
csrftoken = c.cookies['csrftoken']
login_data = dict(j_username=USERNAME, j_password=PASSWORD,           
csrfmiddlewaretoken=csrftoken, next='/')
c.post(url, data=login_data, headers=dict(Referer=url))
page = c.get('http://subscriber.hoovers.com/H/home/index.html')
print(page.content)

以下是表单中的表单数据帖子登录页面:

j_username:user
j_password:pass
OWASP_CSRFTOKEN:8N0Z-TND5-NV71-C4N4-43BK-B13S -A1MO-NZQC
OWASP_CSRFTOKEN:8N0Z-TND5-NV71-C4N4-43BK-B13S-A1MO-NZQC

j_username:user j_password:pass OWASP_CSRFTOKEN:8N0Z-TND5-NV71-C4N4-43BK-B13S-A1MO-NZQC OWASP_CSRFTOKEN:8N0Z-TND5-NV71-C4N4-43BK-B13S-A1MO-NZQC

这是我收到的错误

Traceback (most recent call last):
  File "C:/Users/10023539/Desktop/pyscripts/webscraper ex.py", line 9, in <module>
    csrftoken = c.cookies['csrftoken']
  File "C:\Program Files (x86)\Python35-32\Lib\site-packages\requests\cookies.py", line 293, in __getitem__
    return self._find_no_duplicates(name)
  File "C:\Program Files (x86)\Python35-32\Lib\site-packages\requests\cookies.py", line 351, in _find_no_duplicates
    raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='csrftoken', domain=None, path=None"

我相信这个问题有待解决'OWASP_CSRFTOKEN'标签怎么办?在网上的任何地方,都没有找到针对此特定CSRF名称的任何解决方案。我还尝试过删除c.cookies方法,并手动将CSRF代码输入到csrfmiddlewaretoken参数中。我也尝试过更改引荐网址,但仍然遇到相同的错误。

I believe the issue has something to do with the 'OWASP_CSRFTOKEN' label? I haven't found any solutions for this specific CSRF name anywhere online. I've also tried removing the c.cookies method and manually typing in the CSRF code into the csrfmiddlewaretoken argument. I've also tried changing the referal URL around, still getting the same error.

任何帮助将不胜感激。

推荐答案

首先您捕获到 KeyError 异常,这意味着 cookies 字典没有键 csrftoken

First of all you catch KeyError exception, this mean that cookies dictionary have no key csrftoken.

因此,您需要探索自己的响应以找到正确的CSRF令牌cookie名称。
例如,您可以打印所有cookie:

So you need explore your response for find right CSRF token cookie name. For example you can print all cookies:

for key in c.cookies.keys():
    print('%s: %s' % (key, c.cookies[key]))

UPD:实际上您的回复没有CSRF cookie。
您需要在 c.text 中使用 pyquery

UPD: Actually your response have no CSRF cookie. you need look token in your c.text with pyquery

<input type="hidden" name="OWASP_CSRFTOKEN" class="csrfClass" value="X48L-NEYI-CG18-SJOD-VDW9-FGEB-7WIT-88P4">

这篇关于无法使用请求模块登录网站(Python版本3.5.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆