bs4 parse string to tag code example

Example 1: how to convert response to beautifulsoup object

import requests
from bs4 import BeautifulSoup

url = 'https://www.google.com'

response = requests.get(url)

html_file = BeautifulSoup(response.text, "html.parser")

print(html_file.prettify())

Example 2: get title attribute beautiful soup

for body in message.find_all('div', {'class': 'body'}):
  # grab div by class name
  if body.find('div', {'class': 'date'}):
    text = body.find('div', {'class': 'date'})
    # find div by 'title' attribute
    title = text.get('title', 'No title attribute')
    print(title)

Tags:

Misc Example