-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Random anime character info #4553
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
Changes from all commits
29e6ebd
f7bdf5a
9ac182f
4af41d6
0341083
5375cba
f7b7da1
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
from fake_useragent import UserAgent | ||
|
||
headers = {"UserAgent": UserAgent().random} | ||
URL = "https://www.mywaifulist.moe/random" | ||
|
||
|
||
def save_image(image_url: str, image_title: str) -> None: | ||
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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file |
||
""" | ||
Saves the image of anime character | ||
""" | ||
image = requests.get(image_url, headers=headers) | ||
with open(image_title, "wb") as file: | ||
file.write(image.content) | ||
|
||
|
||
def random_anime_character() -> tuple[str, str, str]: | ||
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. As there is no test file in this pull request nor any test function or class in the file 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. As there is no test file in this pull request nor any test function or class in the file |
||
""" | ||
Returns the Title, Description, and Image Title of a random anime character . | ||
""" | ||
soup = BeautifulSoup(requests.get(URL, headers=headers).text, "html.parser") | ||
title = soup.find("meta", attrs={"property": "og:title"}).attrs["content"] | ||
image_url = soup.find("meta", attrs={"property": "og:image"}).attrs["content"] | ||
description = soup.find("p", id="description").get_text() | ||
_, image_extension = os.path.splitext(os.path.basename(image_url)) | ||
image_title = title.strip().replace(" ", "_") | ||
image_title = f"{image_title}{image_extension}" | ||
save_image(image_url, image_title) | ||
return (title, description, image_title) | ||
|
||
|
||
if __name__ == "__main__": | ||
title, desc, image_title = random_anime_character() | ||
print(f"{title}\n\n{desc}\n\nImage saved : {image_title}") |
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.
As there is no test file in this pull request nor any test function or class in the file
web_programming/random_anime_character.py
, please provide doctest for the functionsave_image