-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_mongo2.py
105 lines (94 loc) · 3.83 KB
/
scrape_mongo2.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
from BeautifulSoup import BeautifulSoup, NavigableString, BeautifulStoneSoup
from htmlentitydefs import name2codepoint as n2cp
import HTMLParser
from pymongo import Connection
import os, sys
import re
import urllib
import urllib2
dollaramount = re.compile("[^\d]")
connection = Connection('localhost', 27017)
db = connection['jparty']
def decode_htmlentities(string):#{{{
entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
return entity_re.subn(substitute_entity, string)[0]
#}}}
def substitute_entity(match):
ent = match.group(2)
if match.group(1) == "#":
return unichr(int(ent))
else:
cp = n2cp.get(ent)
if cp:
return unichr(cp)
else:
return match.group()
def process(soupobjs):#{{{
ts = soupobjs.html.findAll('table',attrs={'class':'round'}, text=False)
categorylist = []
for t in ts:
for c in t.contents:
if isinstance(c,NavigableString): continue
categories = c.findAll('td',attrs={'class':'category_name'})
if not categories: continue
for cat in categories:
catname = u''.join([q.string for q in cat.findAll(text=True)])
categorylist.append( {"name":catname, "questions":[] } ) #Category(catname, game_id) )
for round in [(0,'J'),(1,'DJ')]:
for i in xrange(1,7):
for j in xrange(1,6):
question = soupobjs.html.findAll('td',id='clue_' + round[1] + '_' +str(i)+'_' + str(j))
if not question: continue
toptag = question[0].parent.parent;
value = toptag.findAll(lambda tag: tag.name=='td' and tag.get('class','').startswith('clue_value'))[0].string
answer = toptag.findAll('div')[0]['onmouseover']
if answer:
answerobj = BeautifulSoup(answer).findAll(attrs={'class':'correct_response'})[0]
answertxt = decode_htmlentities(u''.join([q.string for q in answerobj.findAll(text=True)]) )
questiontxt= decode_htmlentities(u''.join([q.string for q in question[0].findAll(text=True)]))
links = question[0].findAll("a")
urls = []
if links:
for l in links:
urls.append( l['href'] )
#if urls:
#print urls
dollarvalue = int( re.sub(dollaramount, '', value).strip() )
questiontxt = questiontxt.replace("\\'","'").replace('\\"','"')
answertxt = answertxt.replace("\\'","'").replace('\\"','"')
newQuestion = {"value":dollarvalue,"question":questiontxt,"answer":answertxt}
if urls: newQuestion['urls'] = urls
if value.find('DD') >= 0: newQuestion['isDouble'] = True;
categorylist[i-1 + 6 * round[0]]['questions'].append(newQuestion)
#print questiontxt, answertxt
return categorylist#}}}
def storecategories(cats):
for cat in cats:
db.categories.insert(cat)
def load_dir(directory):
for f in os.listdir(directory):
with open(os.path.join(directory, f), 'r') as tf:
try:
game_id = f.split('_')[1].split('.')[0]
contents = tf.read()
soup = BeautifulSoup(contents)
qs = process(soup)
for q in qs:
q['game_id'] = game_id
yield qs
except Exception, e:
print e
continue
def main(argv):
for x in load_dir(argv[0]):
db.categories.insert(x)
#print len(x)
#tf = open(argv[0], 'r')
#contents = tf.read()
#soup = BeautifulSoup(contents)
#game_id = int( argv[1] )
#qs = process(soup, game_id)
#storecategories(qs)
#print qs
if __name__ =='__main__':main(sys.argv[1:])