python-requests - 无法登录

标签 python python-requests

尝试抓取一些数据,但首先我需要登录。我正在尝试使用python-requests ,这是我到目前为止的代码:

login_url = "https://www.wehelpen.nl/login/"
users_url = "https://www.wehelpen.nl/ik-zoek-hulp/hulpprofielen/"
profile_url = "https://www.wehelpen.nl/profiel/01136/hulpvragen/"

uname = "****"
pword = "****"


def main():
    s = login(uname, pword, login_url)
    page = s.get(users_url)
    print makeUTF8(page.text) # grab html and grep for logged in text to make sure!


def login(uname, pword, url):
    s = requests.session()
    s.get(url, auth=(uname, pword))
    csrftoken = s.cookies['csrftoken']
    login_data = dict(username=uname, password=pword,
                  csrfmiddlewaretoken=csrftoken, next='/')
    s.post(url, data=login_data, headers=dict(Referer=url))
    return s


def makeUTF8(text):
    return text.encode('utf-8')

基本上,我需要使用 POST 请求登录 login_url (使用 csrf token ,否则我会收到错误),然后使用从 login() 传回的 session 对象,我想通过向用户页面发出 GET 请求来检查我是否已登录。当我得到返回 - page.text 时,我可以运行 grep 命令来检查某个 href 它告诉我是否已登录在或不在。

所以,到目前为止我无法登录并保留工作 session 对象。谁能帮我?到目前为止,这是我一生中最乏味的 Python 经历。

编辑。我已经搜索、搜索、搜索答案,但没有任何效果......

最佳答案

您需要为字典键提供正确的名称。请求库使用表单的 html 名称来查找正确的表单。在您的情况下,这些名称是标识和密码。

login_data = {'identification'=uname,'password'=pswrd...}

关于python-requests - 无法登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18751649/

相关文章:

python - 连续目录名称 Python 3

python - 如何使用 PIL/Pillow 读取 zip 文件中的图像

python-2.7 - Python 请求 - 在 header 和获取请求中使用变量

javascript - YouTube 登录页面上的 javascript 如何发送表单数据?

python请求: SSL error during requests?

python - 使用 Beautifulsoup 和选择器检索内容

python - 值错误 : Cannot set a frame with no defined index and a value that cannot be converted to a Series

python - PyCharm调试时如何避免进入内置函数?

python - 如何更改表和外键的架构?

python - 尝试使用 Python 下载文件时出错