-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathjokes.py
More file actions
executable file
·30 lines (22 loc) · 897 Bytes
/
jokes.py
File metadata and controls
executable file
·30 lines (22 loc) · 897 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
# /usr/bin/env python3
# Imports
import requests
from bs4 import BeautifulSoup
import random
# The concept of webscraping is used here.
# From the URL mentioned below, jokes are scraped and stored.
# A random joke is retrieved
def get_jokes():
jokes = []
url = "https://www.boredpanda.com/funny-pun-jokes/?utm_source=google&utm_medium=organic&utm_campaign=organic"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
for i in (
soup.find("div", attrs={"class": "left-content-column", "data-role": "swipe"})
.find("div", attrs={"class": "open-list-items clearfix"})
.find_all("div", attrs={"class": "open-list-item open-list-block clearfix"})
):
jokes.append((i.find("p").find("span").string))
return random.choice(jokes)
if __name__ == "__main__":
print(get_jokes())