Skip to content

Requests and beautifulsoup

extracting token from within html response

<input type="hidden" name="token" value="a1619cf7cdb1e88230bf406438aa41fdc403dff31967f513f359259f99560afc" />
import requests
from bs4 import BeautifulSoup
url = "http://10.10.10.XX"
cookies = { 'PHPSESSID': 'Cookie_value' }
s = requests.session()

def getToken():
    r = s.get(url, cookies=cookies)
    soup = BeautifulSoup(r.content, "html.parser")
    return soup.find("input", {"name":"token"})["value"]

BeautifulSoup - find element with given class:

mydivs = soup.findAll("div", {"class": "stylelistrow"})

use raw_input to take user input (without having to use quotes)

while True:
    x = raw_input(">>> ")
    # DO ALL THE STUFF

url encoding a string

import urllib
urllib.quote(string_to_encode)

Suppress InsecureRequestWarning from within requets

from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)