[Python] How to extract header paramaters to login to website with BS4? [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to login to a website that I have opened with BeautifulSoup in Python. I attached an image with the form data that I submitted to the websites login page, retrieved from Chrome's network tab. Did I format the data correctly and is there another way that I should be submitting requests to websites to login?

    params = {
    'state': "hKFo2SB0bEYzU09nLVJOS0JZWVI0Y0h4QzdYeGdIQU9XREE0TKFur3VuaXZlcnNhbC1sb2dpbqN0aWTZIEN6eGFVdnNTLVdyQTg1TUw0dlVVRV9fUWc3elh4cVZKo2NpZNkgRVpqa2R1Tk9BaThiajV3SURwVGFaVnJFYVFJZEhhZ0g",
    'email': email,
    'password': password,
    'action': 'default'
    }

    args = urllib.parse.urlencode(params).encode('utf-8')
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
    request = urllib.request.Request(url, data=args, headers=headers)
    with urllib.request.urlopen(request) as response:
    soup = BeautifulSoup(response.read(), 'html.parser')
    print(soup.prettify())


    Screenshot of form data I am trying to submit

    When I run the code, this is the output:

    raceback (most recent call last):
    File "c:\Users\maxim\Nepeto-Bot\main.py", line 23, in <module>
    with urllib.request.urlopen(request, context = context) as response:
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\maxim\AppData\Local\Programs\Python\Python313\Lib\urllib\request.py", line 189, in urlopen
    return opener.open(url, data, timeout)
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\maxim\AppData\Local\Programs\Python\Python313\Lib\urllib\request.py", line 495, in open
    response = meth(req, response)
    File "C:\Users\maxim\AppData\Local\Programs\Python\Python313\Lib\urllib\request.py", line 604, in http_response
    response = self.parent.error(
    'http', request, response, code, msg, hdrs)
    File "C:\Users\maxim\AppData\Local\Programs\Python\Python313\Lib\urllib\request.py", line 533, in error
    return self._call_chain(*args)
    ~~~~~~~~~~~~~~~~^^^^^^^
    File "C:\Users\maxim\AppData\Local\Programs\Python\Python313\Lib\urllib\request.py", line 466, in _call_chain
    result = func(*args)
    File "C:\Users\maxim\AppData\Local\Programs\Python\Python313\Lib\urllib\request.py", line 613, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 400: Bad Request

    Continue reading...

Compartilhe esta Página