diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d8f0350 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.mypy_cache +.venv +.vscode +scraped_quotes.csv \ 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..613c913 100644 --- a/basic_scrape_csv_export.py +++ b/basic_scrape_csv_export.py @@ -1,35 +1,37 @@ -#IMPORT LIBRARIES +# 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 +with open("scraped_quotes.csv", "w", encoding="utf-8") as file: + + # CREATE A VARIABLE FOR WRITING TO THE CSV + writer = csv.writer(file) + + # 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") + + # 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"}) + + # 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) -#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']) - -#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.findAll('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"}) - -#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 - writer.writerow([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])