Skip to content

Instantly share code, notes, and snippets.

@theSoberSobber
Created June 19, 2023 06:35
Show Gist options
  • Select an option

  • Save theSoberSobber/736fcebe8180d1832f248c9c544f7a55 to your computer and use it in GitHub Desktop.

Select an option

Save theSoberSobber/736fcebe8180d1832f248c9c544f7a55 to your computer and use it in GitHub Desktop.
Scrapes site and filters contest using Regex
# importing the required libraries.
import requests
from bs4 import BeautifulSoup
import re
# Requesting the HTML from the web page.
page = requests.get("https://books.toscrape.com/")
# Selecting the data.
soup = BeautifulSoup(page.content, "html.parser")
content = soup.find_all(class_="product_pod")
content = str(content)
# Processing the data using Regular Expressions.
re_titles = r'title="(.*?)">'
titles_list = re.findall(re_titles, content)
re_prices = "£(.*?)</p>"
price_list = re.findall(re_prices, content)
# Saving the output.
with open("output.txt", "w") as f:
for title, price in zip(titles_list, price_list):
f.write(title + "\t" + price + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment