Python
beautiful soup
Oct 11, 2019     1 minutes read

1. What is beautiful soup and why would you use it?

btw. I love the name. It sounds so randomly.

2. The basics

Beautiful Soup can “understand” html code, which you download from the internet using requests module:

import requests
html = requests.get('https://google.com')

unless you look down on mainstream packages and prefer to use something exotic. Then you provide the html to bs in the following way:

from bs4 import BeautifulSoup
soup = BeautifulSoup(product_page.content, 'html.parser')

and from now on you will be able to use all the goodness of bs:

class_book = soup.find('div', {'class': 'book'})

You can also treat some tags as dictionaries, e.g.:

a = page.find("div", {"class": "menu"}).find('a')
url = a['href']
text = a.get_text()