-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnapple.py
More file actions
executable file
·38 lines (29 loc) · 968 Bytes
/
snapple.py
File metadata and controls
executable file
·38 lines (29 loc) · 968 Bytes
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
#!/usr/bin/env python3
"""
@file snapple.py
@brief Gets a random True Fact from Snapple.
@author Evan Elias Young
@date 2017-10-25
@date 2022-02-04
@copyright Copyright 2022 Evan Elias Young. All rights reserved.
"""
from random import choice
import requests as req
from bs4 import BeautifulSoup as bs
def get_fact(fact: int = -1) -> tuple[int, str]:
"""Will fetch a fact from a fact number.
Args:
fact {int}: The number of the fact. (default: {-1})
Returns:
tuple[int, str] -- The fact number and the fact.
"""
res: req.Response = req.get("https://www.snapple.com/real-facts")
soup: bs = bs(res.text, "html.parser")
if fact < 0:
elm = choice(soup.find(id="fact-list").findAll("li"))
else:
elm = soup.find(id="fact-list").find(value=fact)
return (int(elm.attrs["value"]), elm.find("a").text)
if __name__ == "__main__":
print("Hello Console!")
print(get_fact())