How to log in to a website using Python's Requests module?
If the information you want is on the page you are directed to immediately after login...
Lets call your ck
variable payload
instead, like in the python-requests docs:
payload = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'}
url = 'http://www.locationary.com/home/index2.jsp'
requests.post(url, data=payload)
Otherwise...
See https://stackoverflow.com/a/17633072/111362 below.
Login to website using python requests
As said above, you should send values of all fields of form. Those can be find in the Web inspector of browser. This form send 2 addition hidden values:
url = "https://www.voxbeam.com//login"
data = {'userName':'xxxxxxxxx','password':'yyyyyyyyy','challenge':'zzzzzzzzz','hash':''}
# note that in email have encoded '@' like uuuuuuu%40gmail.com
session = requests.Session()
r = session.post(url, headers=headers, data=data)
Also, many sites have protection from a bot like hidden form fields, js, send encoded values, etc. As variants you could:
1) Use a cookies from manual login:
url = "https://www.voxbeam.com"
headers = {'user-agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36"}
cookies = {'PHPSESSID':'zzzzzzzzzzzzzzz', 'loggedIn':'yes'}
s = requests.Session()
r = s.post(url, headers=headers, cookies=cookies)
2) Use module Selenium:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
url = "https://www.voxbeam.com//login"
driver = webdriver.Firefox()
driver.get(url)
u = driver.find_element_by_name('userName')
u.send_keys('xxxxxxxxx')
p = driver.find_element_by_name('password')
p.send_keys('yyyyyyyyy')
p.send_keys(Keys.RETURN)
Logging to a website using Python's requests
You are sending the post request to the wrong url. If you use developer tools to inspect the login form you can get the action
attribute of the form.
In the network tab in developer tools you can see the POST request being made and the parameters. You should make the post request to https://jidelna.mgo.opava.cz:6204/j_spring_security_check
If all of these does not work, also consider emulating the headers as far as possible. There is a cookie being sent, so you might have to use session with Requests.
If everything else fails there is always selenium.
Log in to website using Python Requests module
As @hlt have commented, you must name field the same, as they are named in the form.
Also server may validate "Remember Username" checkbox, so it is better to include it in your request.
payload_login = {
'proxyUsername': username,
'proxyPassword': password,
'proxyRememberUser': true
}
If this does not work for you, it means what site send auth-data is different way. For example, some JS-script may add hidden data in request, or encode some fields.
To find it out, you need to search this HTTP-request in your Browser's Developter Panel or in a external HTTP-sniffer (like Fiddler).
Related Topics
Loop "Forgets" to Remove Some Items
Pretty-Print a Numpy Array Without Scientific Notation and With Given Precision
Set Value For Particular Cell in Pandas Dataframe Using Index
What Is the Syntax Rule For Having Trailing Commas in Tuple Definitions
How to Initialize a Dictionary of Distinct Empty Lists in Python
Pygame Window Not Responding After a Few Seconds
How to Execute a String Containing Python Code in Python
What Are the Advantages of Numpy Over Regular Python Lists
Are For-Loops in Pandas Really Bad? When Should I Care
How to Get the Source Code of a Python Function
Finding Local Ip Addresses Using Python'S Stdlib
Changing One Character in a String
Adding a New Pandas Column With Mapped Value from a Dictionary
Choosing the Correct Upper and Lower Hsv Boundaries For Color Detection With'Cv::Inrange' (Opencv)
How to Find Overlapping Matches With a Regexp
How to Replace Nans by Preceding or Next Values in Pandas Dataframe