forked from HarshCasper/Rotten-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jokes.py
executable file
·26 lines (18 loc) · 862 Bytes
/
jokes.py
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
# /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())