Joseph Straceski Joseph Straceski - 3 months ago 14
Python Question

How to get a uuid with python requests

I am attempting to log into a web page using the python requests module but the Post data on the website that I want to log into includes a uuid tag.

pass: ********
user: ********
uuid: ********


I have thoroughly searched for any mention of this anywhere in most requests documentation. Is this simply out of the capabilities of the program or is it something that I am overlooking.

This is the code I am using.

import requests

url = 'www.website.com'

with requests.Session() as c:
c.get(url)
values = {'pass': 'passsword', 'user': 'username'}
response = c.post(url, data=values)
print response

Answer

You can parse it from the source:

In [29]: from bs4 import BeautifulSoup

In [30]: import re

In [31]: patt = re.compile("document.cplogin.uuid.value=\"(.*?)\"")

In [32]: with requests.Session() as s:
   ....:         page = s.get('http://myneu.neu.edu/cp/home/displaylogin').content
   ....:         soup = BeautifulSoup(page, "html.parser")
   ....:         script = soup.find("script", language="javascript1.1")
   ....:         uuid = patt.search(script.text).group(1)
   ....:     

In [33]: uuid
Out[33]: u'ff3e7ddd-0823-4f44-a003-0e68a9321e08'

If you look at the source of the login page, inside the script with the attribute language="javascript1.1", you can see the uuid:

function login()
{
    setQueryAsCookie();
    document.cplogin.user.value=document.userid.user.value;
    document.cplogin.uuid.value="21fbc26a-3a3d-4802-ba4a-39a40aad881c";
    document.cplogin.submit();
}

So just pass that along with the rest of the form data when you post.

The post url also seems to be https://myneu.neu.edu/cp/home/login, so:

from bs4 import BeautifulSoup
import re

patt = re.compile("document.cplogin.uuid.value=\"(.*?)\"")
data = {"user":"uname", "pass":"passw"}
post = "https://myneu.neu.edu/cp/home/login"

with requests.Session() as s:
    page = s.get('http://myneu.neu.edu/cp/home/displaylogin')
    soup = BeautifulSoup(page.content, "html.parser")
    script = soup.find("script", language="javascript1.1")
    uuid = patt.search(script.text).group(1)
    data["uuid"] = uuid
    resp = s.post(post, data=data)