forked from taivop/joke-dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stupidstuff.py
59 lines (42 loc) · 1.81 KB
/
stupidstuff.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from lxml import html
import requests
import json
import logging
import re
re_category_rating = re.compile(r"\s*Category: (.*[A-z])\s*Rating: (.*\d)\s*")
logging.basicConfig(level=logging.ERROR)
def extract_joke(id):
"""Download and parse a single joke."""
url_base = "http://stupidstuff.org/jokes/joke.htm?jokeid={}"
response = requests.get(url_base.format(id))
tree = html.fromstring(response.content)
content = tree.xpath('//table[@bgcolor="#ffffff" and @width="470"]//table[@class="scroll"]//td')[0]
category_rating_cells = content.xpath('//table[@bgcolor="#ffffff"]//table[@class="bkline"]//td/b[text()="Category: "]/..')
#print(category_rating_cell)
crap = content.xpath('./child::node()[not(self::text()) and not(self::br)]') # all html nodes in content, but not plaintext
for node in crap:
content.remove(node)
body_text = content.text_content().strip()
joke_body = body_text
cell_text = category_rating_cells[0].text_content()
match = re_category_rating.search(cell_text)
category = match.group(1)
rating = float(match.group(2))
return joke_body, category, rating
if __name__ == "__main__":
jokes = []
save_frequency = 100 # save after every 100 IDs
max_id = 3773
for id in range(1, max_id+1): #19000
try:
body, category, rating = extract_joke(id)
joke = {"id": id, "category": category, "body": body, "rating": rating}
jokes.append(joke)
print("ID {} success: [{}]".format(id, category))
except Exception as ex:
print("ID {} failed: ".format(id))
logging.error(ex)
raise ex
if id % save_frequency == 0 or id == max_id:
with open("stupidstuff.json", "w") as f:
json.dump(jokes, f, indent=4, sort_keys=True)