-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathriddles.py
More file actions
executable file
·45 lines (37 loc) · 1.16 KB
/
riddles.py
File metadata and controls
executable file
·45 lines (37 loc) · 1.16 KB
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
# Imports
import requests
from bs4 import BeautifulSoup
import random
# The concept of webscraping is used here.
# From the URL mentioned below, riddles are scraped and stored.
# A random riddles is retrieved
def get_riddles():
url = "https://www.skiptomylou.org/kids-jokes/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
riddles = []
for i in (
soup.find("main", attrs={"class": "content"})
.find("div", attrs={"class": "entry-content"})
.find_all("p")
):
if i is not None:
riddles.append(
str(i.text)
.replace("A", "\b")
.replace("Q", "")
.strip(":")
.replace("\xa0", "")
.replace(":", "")
.replace("\x08", "")
)
actualridd = []
for i in riddles[5:-7]:
p = i.strip().strip(".").split("? ")
try:
actualridd.append(p[0] + "?" + p[1])
except:
actualridd.append(p[0])
return random.choice(actualridd)
if __name__ == "__main__":
print(get_riddles())