From 255d27a01a746604b15f91a57ce30adc4f7fe3d2 Mon Sep 17 00:00:00 2001 From: Gabriel-H189 Date: Sun, 26 Apr 2026 16:55:34 +0100 Subject: [PATCH 1/3] Use find_all instead of findAll --- .gitignore | 3 +++ basic_scrape.py | 4 ++-- basic_scrape_csv_export.py | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fdeb10c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.mypy_cache +.venv +.vscode \ No newline at end of file diff --git a/basic_scrape.py b/basic_scrape.py index 6a4afcd..f847122 100644 --- a/basic_scrape.py +++ b/basic_scrape.py @@ -10,11 +10,11 @@ #FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'TEXT' #AND STORE THE LIST AS A VARIABLE -quotes = soup.findAll('span', attrs={'class':'text'}) +quotes = soup.find_all('span', attrs={'class': 'text'}) #FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'AUTHOR' #AND STORE THE LIST AS A VARIABLE -authors = soup.findAll('small', attrs={"class":"author"}) +authors = soup.find_all('small', attrs={"class": "author"}) #LOOP THROUGH BOTH LISTS USING THE 'ZIP' FUNCTION #AND PRINT AND FORMAT THE RESULTS diff --git a/basic_scrape_csv_export.py b/basic_scrape_csv_export.py index d036f15..d133410 100644 --- a/basic_scrape_csv_export.py +++ b/basic_scrape_csv_export.py @@ -19,11 +19,11 @@ soup = BeautifulSoup(page_to_scrape.text, 'html.parser') #FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'TEXT' #AND STORE THE LIST AS A VARIABLE -quotes = soup.findAll('span', attrs={'class':'text'}) +quotes = soup.find_all('span', attrs={'class':'text'}) #FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'AUTHOR' #AND STORE THE LIST AS A VARIABLE -authors = soup.findAll('small', attrs={"class":"author"}) +authors = soup.find_all('small', attrs={"class":"author"}) #LOOP THROUGH BOTH LISTS USING THE 'ZIP' FUNCTION #AND PRINT AND FORMAT THE RESULTS From 28949cb00b8e88312edc4c130847d8dbdf45f914 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 22 Aug 2026 10:42:00 +0100 Subject: [PATCH 2/3] Format basic_scrape_csv_export.py --- .gitignore | 3 ++- basic_scrape_csv_export.py | 47 +++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index fdeb10c..d8f0350 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .mypy_cache .venv -.vscode \ No newline at end of file +.vscode +scraped_quotes.csv \ No newline at end of file diff --git a/basic_scrape_csv_export.py b/basic_scrape_csv_export.py index d133410..e2d4506 100644 --- a/basic_scrape_csv_export.py +++ b/basic_scrape_csv_export.py @@ -1,35 +1,40 @@ -#IMPORT LIBRARIES +# IMPORT LIBRARIES from bs4 import BeautifulSoup import requests -#IMPORT CSV LIBRARY +# IMPORT CSV LIBRARY import csv -#OPEN A NEW CSV FILE. IT CAN BE CALLED ANYTHING -file = open('scraped_quotes.csv', 'w') -#CREATE A VARIABLE FOR WRITING TO THE CSV +# OPEN A NEW CSV FILE. IT CAN BE CALLED ANYTHING +file = open("scraped_quotes.csv", "w") + +# CREATE A VARIABLE FOR WRITING TO THE CSV writer = csv.writer(file) -#CREATE THE HEADER ROW OF THE CSV -writer.writerow(['Quote', 'Author']) +# CREATE THE HEADER ROW OF THE CSV +writer.writerow(["Quote", "Author"]) + +# REQUEST WEBPAGE AND STORE IT AS A VARIABLE +page_to_scrape = requests.get("https://quotes.toscrape.com", timeout=30) + +# USE BEAUTIFULSOUP TO PARSE THE HTML AND STORE IT AS A VARIABLE +soup = BeautifulSoup(page_to_scrape.text, "html.parser") -#REQUEST WEBPAGE AND STORE IT AS A VARIABLE -page_to_scrape = requests.get("http://quotes.toscrape.com") -#USE BEAUTIFULSOUP TO PARSE THE HTML AND STORE IT AS A VARIABLE -soup = BeautifulSoup(page_to_scrape.text, 'html.parser') -#FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'TEXT' -#AND STORE THE LIST AS A VARIABLE -quotes = soup.find_all('span', attrs={'class':'text'}) +# FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'TEXT' +# AND STORE THE LIST AS A VARIABLE +quotes = soup.find_all("span", attrs={"class": "text"}) -#FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'AUTHOR' -#AND STORE THE LIST AS A VARIABLE -authors = soup.find_all('small', attrs={"class":"author"}) +# FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'AUTHOR' +# AND STORE THE LIST AS A VARIABLE +authors = soup.find_all("small", attrs={"class": "author"}) -#LOOP THROUGH BOTH LISTS USING THE 'ZIP' FUNCTION -#AND PRINT AND FORMAT THE RESULTS +# LOOP THROUGH BOTH LISTS USING THE 'ZIP' FUNCTION +# AND PRINT AND FORMAT THE RESULTS for quote, author in zip(quotes, authors): print(quote.text + "-" + author.text) - #WRITE EACH ITEM AS A NEW ROW IN THE CSV + + # WRITE EACH ITEM AS A NEW ROW IN THE CSV writer.writerow([quote.text, author.text]) -#CLOSE THE CSV FILE + +# CLOSE THE CSV FILE file.close() From 4ae5119e29d04fe2722686cd1454d88692cd6b02 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 22 Aug 2026 10:43:14 +0100 Subject: [PATCH 3/3] Use with statement --- basic_scrape_csv_export.py | 51 ++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/basic_scrape_csv_export.py b/basic_scrape_csv_export.py index e2d4506..613c913 100644 --- a/basic_scrape_csv_export.py +++ b/basic_scrape_csv_export.py @@ -1,40 +1,37 @@ +# IMPORT CSV LIBRARY +import csv + # IMPORT LIBRARIES from bs4 import BeautifulSoup import requests -# IMPORT CSV LIBRARY -import csv - # OPEN A NEW CSV FILE. IT CAN BE CALLED ANYTHING -file = open("scraped_quotes.csv", "w") - -# CREATE A VARIABLE FOR WRITING TO THE CSV -writer = csv.writer(file) +with open("scraped_quotes.csv", "w", encoding="utf-8") as file: -# CREATE THE HEADER ROW OF THE CSV -writer.writerow(["Quote", "Author"]) + # CREATE A VARIABLE FOR WRITING TO THE CSV + writer = csv.writer(file) -# REQUEST WEBPAGE AND STORE IT AS A VARIABLE -page_to_scrape = requests.get("https://quotes.toscrape.com", timeout=30) + # CREATE THE HEADER ROW OF THE CSV + writer.writerow(["Quote", "Author"]) -# USE BEAUTIFULSOUP TO PARSE THE HTML AND STORE IT AS A VARIABLE -soup = BeautifulSoup(page_to_scrape.text, "html.parser") + # REQUEST WEBPAGE AND STORE IT AS A VARIABLE + page_to_scrape = requests.get("https://quotes.toscrape.com", timeout=30) -# FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'TEXT' -# AND STORE THE LIST AS A VARIABLE -quotes = soup.find_all("span", attrs={"class": "text"}) + # USE BEAUTIFULSOUP TO PARSE THE HTML AND STORE IT AS A VARIABLE + soup = BeautifulSoup(page_to_scrape.text, "html.parser") -# FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'AUTHOR' -# AND STORE THE LIST AS A VARIABLE -authors = soup.find_all("small", attrs={"class": "author"}) + # FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'TEXT' + # AND STORE THE LIST AS A VARIABLE + quotes = soup.find_all("span", attrs={"class": "text"}) -# LOOP THROUGH BOTH LISTS USING THE 'ZIP' FUNCTION -# AND PRINT AND FORMAT THE RESULTS -for quote, author in zip(quotes, authors): - print(quote.text + "-" + author.text) + # FIND ALL THE ITEMS IN THE PAGE WITH A CLASS ATTRIBUTE OF 'AUTHOR' + # AND STORE THE LIST AS A VARIABLE + authors = soup.find_all("small", attrs={"class": "author"}) - # WRITE EACH ITEM AS A NEW ROW IN THE CSV - writer.writerow([quote.text, author.text]) + # LOOP THROUGH BOTH LISTS USING THE 'ZIP' FUNCTION + # AND PRINT AND FORMAT THE RESULTS + for quote, author in zip(quotes, authors): + print(quote.text + "-" + author.text) -# CLOSE THE CSV FILE -file.close() + # WRITE EACH ITEM AS A NEW ROW IN THE CSV + writer.writerow([quote.text, author.text])