Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion app/parse.py
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
Expand All @@ -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")]

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 that find_all returns a non-empty list before accessing [0].text to avoid potential IndexError.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requests.get(url).content should be checked for a successful response status before accessing .content. Consider using response = requests.get(url) and then checking response.status_code to ensure it's 200 before proceeding.

soup = BeautifulSoup(response, "html.parser")

if page_with_quotes(soup):
block_quote = page_with_quotes(soup)
Comment on lines +66 to +67

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function page_with_quotes is called twice here. You can optimize this by calling it once and storing the result in a variable, then using that variable in the condition and the subsequent code.

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}")

Choose a reason for hiding this comment

The 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__":
Expand Down
Binary file modified requirements.txt
Binary file not shown.
Loading