0

I've read many posts on the Internet, but none of them seems to work for me. I have a web form which requires an username and a password in order to log in. Here's a snippet of code among those I tried so far

params = urllib.parse.urlencode({'login' : 'user', 'password' : 'pwd'})
f = urllib.request.urlopen("https://example.com/login.php?%s" % params)
print(f.read().decode('utf-8'))

However it gives me back the login page source instead of the "user panel" that appears after log in. If I write https:/example.com/login.php?login=user&password=pwd in the url bar of the browser, it logs me in. The goal is to get the page source that appears after logging in (the "user panel"). Can anyone help me please?

Using Python 3.5

Selcuk
60.1k12 gold badges114 silver badges119 bronze badges
asked Jun 3, 2016 at 9:01
5
  • Maybe the login just redirects you to the user panel after the login or maybe you are missing some headers on the request Commented Jun 3, 2016 at 9:05
  • 2
    You are making a GET request instead of a POST. Commented Jun 3, 2016 at 9:10
  • Do yourself a favour and use requests, docs.python-requests.org/en/master/user/quickstart Commented Jun 3, 2016 at 9:15
  • @PadraicCunningham I've been trying to make it work for 20 mins on Windows, let me try on Unix Commented Jun 3, 2016 at 9:37
  • @LucaMozzo, can you share the url? Commented Jun 3, 2016 at 22:44

2 Answers 2

1

The correct usage is

f = urllib.request.urlopen("https://example.com/login.php", data=params)

From urllib.request documentation:

data may be a string specifying additional data to send to the server, or None if no such data is needed.

Note that this will send a POST request instead of a GET. If you have to send a GET request, you can simply do the following:

f = urllib.request.urlopen("https://example.com/login.php?user={0}&password={1}".format('user', 'pwd'))
answered Jun 3, 2016 at 9:06
Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately I still get back the login page also by doung in this way (it asks me an array of bytes so I had to write data=params.encode("utf-8") instead)
@LucaMozzo The site might require a GET request. Did you try the second method?
The HTML says "method=post", but I've just tried it and still nothing!
@LucaMozzo if you use the last line from Selcuk's answer it should be the same as if you put it in the browser url bar. Have you run that line of code?
1

According to the documentation for urllib, you need to pass keyword argument data (or the second positional argument) to urlopen, instead of adding them to the url:

import urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
print f.read()
answered Jun 3, 2016 at 9:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.