Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Mike323334
Python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python
/
web_programming
/
random_anime_character.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
48 lines (39 loc) · 1.42 KB
master
Breadcrumbs
Python
/
web_programming
/
random_anime_character.py
Copy path
Top
File metadata and controls
Code
Blame
48 lines (39 loc) · 1.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "beautifulsoup4",
# "fake-useragent",
# "httpx2",
# ]
# ///
import
os
import
httpx2
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
:
"""
Saves the image of anime character
"""
image
=
httpx2
.
get
(
image_url
,
headers
=
headers
,
timeout
=
10
)
with
open
(
image_title
,
"wb"
)
as
file
:
file
.
write
(
image
.
content
)
def
random_anime_character
()
->
tuple
[
str
,
str
,
str
]:
"""
Returns the Title, Description, and Image Title of a random anime character .
"""
soup
=
BeautifulSoup
(
httpx2
.
get
(
URL
,
headers
=
headers
,
timeout
=
10
).
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
\n
Image saved :
{
image_title
}
"
)
You can’t perform that action at this time.