-
Notifications
You must be signed in to change notification settings - Fork 377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Develop #366
base: master
Are you sure you want to change the base?
Develop #366
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import csv | ||
import dataclasses | ||
from dataclasses import dataclass | ||
import requests | ||
from bs4 import BeautifulSoup, Tag, ResultSet | ||
|
||
HOME_PAGE_URL = "https://quotes.toscrape.com" | ||
|
||
|
||
@dataclass | ||
|
@@ -8,8 +14,51 @@ class Quote: | |
tags: list[str] | ||
|
||
|
||
def get_single_quote(single_quote: Tag) -> Quote: | ||
text = single_quote.find_all("span", class_="text")[0].text | ||
author = single_quote.find_all("small", class_="author")[0].text | ||
tags = [elem.text for elem in single_quote.find_all("a", class_="tag")] | ||
return Quote(text=text, author=author, tags=tags) | ||
|
||
|
||
def page_with_quotes(soup_page: BeautifulSoup) -> ResultSet: | ||
block_quote = soup_page.select(".quote") | ||
return block_quote | ||
|
||
|
||
def next_page(page: BeautifulSoup) -> str | None: | ||
page = page.find("li", class_="next") | ||
if page: | ||
tag_a = page.find("a") | ||
if tag_a and "href" in tag_a.attrs: | ||
return HOME_PAGE_URL + tag_a.get("href") | ||
return None | ||
|
||
|
||
def main(output_csv_path: str) -> None: | ||
pass | ||
all_quotes = [] | ||
url = HOME_PAGE_URL | ||
while url: | ||
try: | ||
response = requests.get(url).content | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
soup = BeautifulSoup(response, "html.parser") | ||
|
||
if page_with_quotes(soup): | ||
block_quote = page_with_quotes(soup) | ||
Comment on lines
+66
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function |
||
parsed_quotes = [get_single_quote(info) | ||
for info in block_quote] | ||
all_quotes.extend(quotes.__dict__ for quotes in parsed_quotes) | ||
url = next_page(soup) | ||
except requests.RequestException as e: | ||
print(f"When parse got error {e}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message 'When parse got error' is not very descriptive. Consider providing more context or details about the error to aid in debugging. |
||
break | ||
|
||
with open(output_csv_path, "w", newline="", encoding="utf-8") as csvfile: | ||
fieldnames = [attr.name for attr in dataclasses.fields(Quote)] | ||
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | ||
writer.writeheader() | ||
|
||
writer.writerows(all_quotes) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
get_single_quote
function assumes that the elements it searches for will always be present. Consider adding checks to ensure thatfind_all
returns a non-empty list before accessing[0].text
to avoid potentialIndexError
.