Authentication and python requests

I am trying to load some documents using queries, but the page redirects me to the user log screen and loads the HTML page.

I tried:

c=requests.get(url,auth=HTTPBasicAuth('user','pass'))

But I do not get authentication.

I also tried vanilla and digest.

The form itself is as follows:

<input id="username" name="username" class="required" tabindex="1" type="text" value="" size="25" autocomplete="false"/>
<br/>

<label for="password">Password</label>
<input id="password" name="password" class="required" tabindex="2" type="password" value="" size="25" autocomplete="off"/>

Do I need to enter a username and password as part of the payload? If so, how do I do this? I tried several different ways.

+5
source share
1 answer

In principle, this is due to the capture of the authentication identifier from the page and the transfer of cookies.

This is basically what I did:

from bs4 import BeautifulSoup as bs
import requests
s = requests.session()
url = r'url_i_care_about'

def authenticate(s, url):
    headers = {'username': 'myuser', 'password': 'mypasss', '_Id': 'submit'}
    page=s.get(url)
    soup=bs(page.content)
    value=soup.form.find_all('input')[2]['value']
    headers.update({'value_name':value})
    auth = s.post(url, params=headers, cookies=page.cookies)
+6
source

All Articles