简体   繁体   中英

Login website with python requests

I'm trying to login to a webpage using python 3 using requests and lxml. However, after sending a post request to the login page, I can't enter pages that are available after login. What am I missing?

import requests
from lxml import html

session_requests = requests.session()

login_URL = 'https://www.voetbal.nl/inloggen'
r = session_requests.get(login_URL)

tree = html.fromstring(r.text)
form_build_id = list(set(tree.xpath("//input[@name='form_build_id']/@value")))[0]

payload = {
    'email':'mom.soccer@mail.com',
    'password':'testaccount',
    'form_build_id':form_build_id
    }

headers = {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7',
    'Cache-Control':'max-age=0',
    'Connection':'keep-alive',
    'Content-Type':'multipart/form-data; boundary=----WebKitFormBoundarymGk1EraI6yqTHktz',
    'Host':'www.voetbal.nl',
    'Origin':'https://www.voetbal.nl',
    'Referer':'https://www.voetbal.nl/inloggen',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
    }

result = session_requests.post(
    login_URL,
    data = payload,
    headers = headers
)

pvc_url = 'https://www.voetbal.nl/club/BBCB10Z/overzicht'
result_pvc = session_requests.get(
    pvc_url,
    headers = headers
)

print(result_pvc.text)

The account in this sample is activated, but it is just a test-account which I created to put my question up here. Feel free to try it out.

Answer:

there where multiple problems:

Payload: 'form_id': 'voetbal_login_login_form' was missing. Thanks @tmadam

Cookies: request cookies where missing. They seem to be static, so I tried to add them manually, which worked. Thanks @match and @Patrick Doyle

Headers: removed the 'content-type' line; which contained a dynamic part.

Login works like a charm now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM