使用python请求库登录网站 [英] Using python requests library to login to website

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

问题描述

我研究了许多SO线程,以了解如何使用请求创建会话库,但是我尝试过的所有方法都没有实际登录.我对Web设计和协议的经验很少,因此请指出我可能需要了解的所有基础知识.这是我在做什么:

I have looked through many SO threads on how to create a session using the requests library, but none of the methods I tried actually log me in. I have very little experience with web design and protocols, so please point out any basics that I might need to understand. Here is what I'm doing:

import requests

EMAIL = 'my_email'
PASSWORD = 'my_pw'
URL = 'https://account.guildwars2.com/login'

session = requests.session()

login_data = dict(username=EMAIL, password=PASSWORD)
r = session.post(URL, data=login_data)

req = session.get('https://leaderboards.guildwars2.com/en/na/achievements/guild/Darkhaven%20Elite')

print req.content

我看到的内容是我未登录时看到的内容.

The content I am seeing is the content that I would see if I were not logged in.

我的语法是否有问题,或者是由于我正在访问的登录页面的设置方式引起的问题?

Is there anything incorrect about my syntax, or is the problem caused by the way the login page I am accessing is set up?

推荐答案

@DanAlbert指出,我告诉您使用的不是您要尝试的HTTP Auth.我认为由于一切看起来正确,因此可能只是您需要的HTTP Auth.

@DanAlbert pointed out that I was telling you to use HTTP Auth which isn't what you're trying to do. I assumed since everything looked correct that it might just be HTTP Auth that you needed.

但是,看一下表格,似乎您在dict中使用的变量名不正确:

However, looking at the form it looks like maybe you're just using the wrong variable name in your dict:

import requests
s = requests.session()
login_data = dict(email='email', password='password')
s.post('https://account.guildwars2.com/login', data=login_data)
r = s.get('https://leaderboards.guildwars2.com/en/na/achievements/guild/Darkhaven%20Elite')
print r.content

如果您查看表格,则期望使用变量"email"和"password".您有用户名"和密码"

If you look at the form it's expecting the variables "email" and "password". You have "username" and "password"

无论如何,HTH

这篇关于使用python请求库登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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