python-requests Exemple d'accès à des pages authentifiées à l'aide de requêtes


Nous avons parfois besoin d'analyser les pages, mais cela nécessite que vous soyez un utilisateur autorisé. Voici un exemple qui montre comment effectuer une connexion à Oracle.

import sys
import requests
import json
from bs4 import BeautifulSoup

def mprint(x):

headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1'}

mprint('[-] Initialization...')
s = requests.session()
print 'done'

mprint('[-] Gathering JSESSIONID..')

# This  should redirect us to the login page 
# On looking at the page source we can find that 
# in the submit form 6 values are submitted (at least at the time of this script)
# try to take those values out using beautiful soup 
# and then do a post request. On doing post 
# we will be given message we have the data which is more than necessary
# then it will take us to the form where we have to submit data here 
# once done we are signed in and doing and requests.get(url) will get you the page you want.

r = s.get("company's local url- a link which requires authentication")
if r.status_code !=
  print 'error'
print 'done'

c = r.content
soup = BeautifulSoup(c,'lxml')
svars = {}

for var in soup.findAll('input',type="hidden"):
    svars[var['name']] = var['value']

s = requests.session()
r ='', data=svars)

mprint('[-] Trying to submit credentials...')
inputRaw = open('credentials.json','r')
login = json.load(inputRaw)

data =  {
        'v': svars['v'],
        'OAM_REQ': svars['OAM_REQ'],
        'site2pstoretoken': svars['site2pstoretoken'],
        'locale': svars['locale'],
        'ssousername': login['ssousername'],
        'password': login['password'],

r ='', data=data)

r = s.get("company's local url- a link which requires authentication")
# dumping the html page to html file
with open('test.html','w') as f:

credentials.json comme mentionné dans le code est comme suit:

  "password":"put your password here"

Lien vers github - gist