Reputation: 173
I am learning python for fun, and my project for that is parsing popular sites with flash deals and posting it to site https://www.pepper.pl/ . I had a look on networking while messing with the site in chrome, and I've found that request body for login contains following data:
_token: gse5bAi58jnciXdynLu7D7ncXmTg1twChWMjsOFF
source: generic_join_button_header
identity: login
password: password
remember: on
So using postman I've filled this data into request with content-type as application/x-www-form-urlencoded. And the response was correct, I was able to login with postman. But when I tried to reproduce that with python, it was a failure, I received 404.
def get_pepper_token():
url = "https://www.pepper.pl/login/modal/login"
request = requests.get(url)
soup = BeautifulSoup(page, features="html.parser")
return soup.find('input', attrs={'name': '_token'})['value']
def get_login_headers():
url = "https://www.pepper.pl/login"
username = 'username'
password = 'password'
token = get_pepper_token()
payload = {
'_token': token,
'source': 'generic_join_button_header',
'identity': username,
'password': password,
'remember': 'on'
}
headers = {
'Content-Type': "application/x-www-form-urlencoded"
}
response = requests.post(url, payload, headers=headers)
So I've monitored in postman console what was exactly in request:
Request Headers:
content-type:"application/x-www-form-urlencoded"
cache-control:"no-cache"
postman-token:"de74adb5-5e9b-4c98-9a95-bb69bc739270"
user-agent:"PostmanRuntime/7.2.0"
accept:"*/*"
cookie:"__cfduid=d32b701203ce16ee47549cbe5388b3faa1534746292; first_visit=%22bf0e1200-a441-11e8-b92e-6805ca619fd2%22; pepper_session=%2255c4b461a56c37f5c2ce1a7323b44f8d12353e91%22; browser_push_permission_requested=1534748540; remember_afba1956ef54387311fa0b0cd07acd2b=%22100085%7ChX2GS7H3l8QY79HasDcB3scptVyKGDVMJHdz4Ux2ONIih6Rp2VKhU0BpxvzD%22; view_layout_horizontal=%220-1%22; show_my_tab=0; navi=%5B%5D"
accept-encoding:"gzip, deflate"
referer:"https://www.pepper.pl/login"
and as you can see there are some fields in request headers which I did not enter in postman. I added manually cookie value from request headers from postman, and it worked. Rest of those fields are not required.
Do you know how I may generate this cookie?
Upvotes: 0
Views: 1457
Reputation: 173
The answer is simple library RoboBrowser, here is how I solved part of the problem: Very short and handy solution compare to my previous attempts. RoboBrowser GitHub page
url = "https://www.pepper.pl/login/modal/login"
browser = RoboBrowser()
browser.open(url)
signup_form = browser.get_form('login_form')
signup_form['identity'].value = self.username
signup_form['password'].value = self.password
browser.submit_form(signup_form)
Upvotes: 1