Reputation: 962
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
Upvotes: 0
Views: 74
Reputation: 59164
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'))
Upvotes: 1
Reputation: 4229
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()
Upvotes: 1