Created
June 19, 2023 06:35
-
-
Save theSoberSobber/736fcebe8180d1832f248c9c544f7a55 to your computer and use it in GitHub Desktop.
Scrapes site and filters contest using Regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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