Using python requests library to login to website

岁酱吖の 提交于 2019-12-18 05:15:07

问题


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?


回答1:


@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.

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

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

Anyway, HTH



来源:https://stackoverflow.com/questions/16994044/using-python-requests-library-to-login-to-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!