4

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?

secretformula
  • 6,414
  • 3
  • 33
  • 56
sebo
  • 1,584
  • 4
  • 16
  • 19
  • 1
    It depends on how the site expects session information to be sent around. Session objects will automatically take care of sessions passed via cookies, but not sessions that require an ID to be passed in other ways, e.g., as a query string on the URL—which happens to be common in older browser games. For those other cases, you have to do it manually. – abarnert Jun 07 '13 at 22:53
  • 1
    Anyway, the first thing to do is to look at the URLs in the address bar when you log into the game in your browser. If there's nothing obvious there, use a browser extension that will log or record the headers sent for each request, to see exactly what's happening there. Once you know what you're missing, it should be easy to write the code that fills it in (or, if it's not easy for you to write, it should at least be easy for others to write after you post it here). – abarnert Jun 07 '13 at 22:55
  • Also, doesn't Guild Wars 2 have an API that you can use, instead of trying to scrape the user-facing browser interface? – abarnert Jun 07 '13 at 22:56
  • 1
    They do, but it seems to be very limited right now. It doesn't provide the functionality needed. http://wiki.guildwars2.com/wiki/API – Dan Albert Jun 07 '13 at 23:16
  • Yes, there is no other way to access the information I am looking for. The "leaderboard" for a particular guild is only available if you are logged in and are a member of that guild. Reproducing these accesses in a browser works fine. The URLs don't change when they are entered, and after logging in the browser redirects me to a generic https://account.guildwars2.com/account/download URL – sebo Jun 08 '13 at 00:00

1 Answers1

9

@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

  • 3
    That will perform HTTP Basic Authentication, which is performed by adding headers to the HTTP request. From what I can tell, he's looking for a way to log in to the site the way he would if he filled out the log in form (which would be a POST). – Dan Albert Jun 07 '13 at 23:09
  • 1
    DanAlbert is correct in pointing out what I'm trying to achieve. – sebo Jun 07 '13 at 23:56
  • @sebo I edited my response shortly after he pointed that out. I'm not sure if you took a look, but the guildwars login is expecting two post variables 'email' and 'password', but you have 'username' and 'password'. I think it will work if you change username to email. –  Jun 08 '13 at 00:54
  • @mchaelavila I tried making the change that you suggested but it still did not log me in as expected. Thanks for your suggestion though. – sebo Jun 08 '13 at 01:05
  • @sebo I tried making an account to test it myself, but it appears to require purchasing a serial number. So I guess I'm out of options. Let us know if you get any new information. Good luck! –  Jun 08 '13 at 01:08
  • I have used the same code with the same site (after creating an account ) but it didn't work for me... – pietà Dec 12 '17 at 10:20