import requests from bs4 import BeautifulSoup # Make a request url = 'https://www.example.com' r = requests.get(url) # Create a BeautifulSoup object soup = BeautifulSoup(r.content, 'html.parser') # Extract the title of the page title = soup.title.string print(title)
from bs4 import BeautifulSoup # Read the XML file with open('file.xml', 'r') as f: xml_data = f.read() # Create a BeautifulSoup object soup = BeautifulSoup(xml_data, 'xml') # Extract the data items = soup.find_all('item') for item in items: name = item.find('name').text price = item.find('price').text print(f'{name} costs {price}.')In both examples, we import the BeautifulSoup package/library (identified by the `from bs4` statement) and create a `BeautifulSoup` object to parse the HTML or XML content. We then use the methods provided by the library to extract the data we need.