-
Notifications
You must be signed in to change notification settings - Fork 378
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
Created a solution #367
Open
MaksymProtsak
wants to merge
11
commits into
mate-academy:master
Choose a base branch
from
MaksymProtsak:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Created a solution #367
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
044eb67
Filled main function in parse.py.
MaksymProtsak bd2bc2f
Update requirements.txt.
MaksymProtsak 2ffe80f
Created get_quotes, pars_single_quote. Updated main.
MaksymProtsak cceec3a
Created get_copu_page, next_page. Updated get_quotes, main functions.
MaksymProtsak 703d331
Created create_page_link. Updated next_page, main.
MaksymProtsak 60686ca
Created QUOTES_FIELDS, write_quotes_to_csv updated if condition in main.
MaksymProtsak 14b68bf
Updated requirements.txt.
MaksymProtsak ece39a0
flake8.
MaksymProtsak 5ec4c53
Updated requirements.txt.
MaksymProtsak c04f320
Updated requirements.txt.
MaksymProtsak 64dfc2e
flake8
MaksymProtsak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
from dataclasses import dataclass | ||
import csv | ||
from dataclasses import ( | ||
dataclass, | ||
fields, astuple | ||
) | ||
from urllib.parse import urljoin | ||
|
||
from bs4 import BeautifulSoup, Tag | ||
|
||
import requests | ||
|
||
BASE_URL = "https://quotes.toscrape.com/" | ||
|
||
|
||
@dataclass | ||
|
@@ -8,8 +19,69 @@ class Quote: | |
tags: list[str] | ||
|
||
|
||
QUOTES_FIELDS = [field.name for field in fields(Quote)] | ||
|
||
|
||
def create_page_link(base_url: str, next_page_url: str) -> str: | ||
return urljoin(base_url, next_page_url) | ||
|
||
|
||
def get_soup_page(url: str) -> BeautifulSoup: | ||
r = requests.get(url, ).content | ||
soup = BeautifulSoup(r, "html.parser") | ||
return soup | ||
|
||
|
||
def next_page(soup: BeautifulSoup) -> dict: | ||
next_class = soup.select(".next") | ||
is_next_page = bool(len(next_class)) | ||
next_page_link = None | ||
if is_next_page: | ||
next_page_link = soup.select(".next")[0].a.attrs["href"] | ||
return { | ||
"is_next_page": is_next_page, | ||
"next_page_link": next_page_link, | ||
} | ||
|
||
|
||
def get_quotes(soup_page: BeautifulSoup) -> list[Tag]: | ||
quotes = soup_page.select(".quote") | ||
return quotes | ||
|
||
|
||
def pars_single_quote(quote: Tag) -> Quote: | ||
text = quote.select(".text")[0].contents[0] | ||
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 name |
||
author = quote.select(".author")[0].contents[0] | ||
tags = [str(tag.contents[0]) for tag in quote.select(".tag")] | ||
return Quote(text=str(text), author=str(author), tags=list(tags)) | ||
|
||
|
||
def write_quotes_to_csv(quotes, output_csv_path): | ||
with open(output_csv_path, "w", encoding="utf-8", newline="") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(QUOTES_FIELDS) | ||
writer.writerows([astuple(quote) for quote in quotes]) | ||
|
||
|
||
def main(output_csv_path: str) -> None: | ||
pass | ||
page_link = create_page_link(BASE_URL, "") | ||
bs_page = get_soup_page(page_link) | ||
is_next_page = next_page(bs_page) | ||
quotes = get_quotes(bs_page) | ||
parsed_quotes = [] | ||
while True: | ||
for quote in quotes: | ||
parsed_quotes.append( | ||
pars_single_quote(quote) | ||
) | ||
if not is_next_page["is_next_page"]: | ||
break | ||
page_link = create_page_link(BASE_URL, is_next_page["next_page_link"]) | ||
bs_page = get_soup_page(page_link) | ||
is_next_page = next_page(bs_page) | ||
quotes = get_quotes(bs_page) | ||
|
||
write_quotes_to_csv(parsed_quotes, output_csv_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
flake8==5.0.4 | ||
flake8-annotations==2.9.1 | ||
flake8-quotes==3.3.1 | ||
flake8-variables-names==0.0.5 | ||
pep8-naming==0.13.2 | ||
pytest==7.1.3 | ||
beautifulsoup4==4.12.3 | ||
certifi==2024.12.14 | ||
charset-normalizer==3.4.1 | ||
idna==3.10 | ||
requests==2.32.3 | ||
soupsieve==2.6 | ||
urllib3==2.3.0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is an unnecessary comma after
requests.get(url, )
. It should be removed to avoid confusion.