forked from taivop/joke-dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wocka.py
63 lines (45 loc) · 1.95 KB
/
wocka.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
60
61
62
63
from lxml import html
import requests
import json
import logging
logging.basicConfig(level=logging.ERROR)
def extract_joke(id):
"""Download and parse a single joke."""
url_base = "http://www.wocka.com/{}.html"
response = requests.get(url_base.format(id))
tree = html.fromstring(response.content)
content = tree.xpath('//div[@id="content"]')[0]
h2s = tree.xpath('//div[@id="content"]/h2')
category_rows = content.xpath('./div[@class="right"]//tr/td/b[text()="Category"]/../..')
crap = tree.xpath('//div[@id="content"]/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()
if body_text == "This joke does not exist":
return None, "does not exist", None
if body_text == "This is a dirty joke, so it has been hidden. To read this joke, you will need to create an account and signin.":
return None, "is dirty", None
category = category_rows[0].xpath('./td/a/text()')[0]
title = h2s[0]
joke_title = title.text_content()
joke_body = body_text
return joke_title, joke_body, category
if __name__ == "__main__":
jokes = []
save_frequency = 100 # save after every 100 IDs
max_id = 19000
for id in range(1, max_id+1): #19000
try:
title, body, category = extract_joke(id)
if title is None:
print("ID {} {}.".format(id, body))
continue
joke = {"id": id, "category": category, "title": title, "body": body}
jokes.append(joke)
print("ID {} success: [{}] {}".format(id, category, title))
except Exception as ex:
print("ID {} failed: ".format(id))
logging.error(ex)
if id % save_frequency == 0 or id == max_id:
with open("wocka.json", "w") as f:
json.dump(jokes, f, indent=4, sort_keys=True)