-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.py
155 lines (109 loc) · 2.73 KB
/
scrape.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import urllib
from bs4 import BeautifulSoup
import ipdb
import pymongo
from pymongo import MongoClient
import time
class Node(object):
def __init__(self, name = '', size = 0, children = None):
self.name = name
self.size = 0
self.nodeset = dict()
def set_size(self, size):
self.size = size
def get_set(self):
return self.nodeset;
def __eq__(self, other):
return self.name == other.name
def __hash__(self):
return hash(self.name)
class Tree(object):
def __init__(self):
self.root = Node()
def insert(self, name, size):
children = self.root.get_set()
names = name.split(" > ")
for x in range(0, len(names)):
curr = names[x]
if children.has_key(curr):
child = children.get(curr)
if x == len(names) - 1:
child.set_size(size)
else:
new_node = Node(curr, 0)
if x == len(names) - 1:
new_node.set_size(size)
children[curr] = new_node
children = children[curr].get_set()
def traverse(self, node):
# ipdb.set_trace()
l = []
l.append("{")
# l.append(string)
l.append('"name":"')
l.append(node.name)
l.append('",')
l.append('"size":"')
l.append(str(node.size))
l.append('",')
# l.append(string)
l.append('"children":[')
for x in node.get_set().values():
# l.append(string)
l.append(self.traverse(x))
l.append(",")
if len(node.get_set().values()) > 0:
l = l[:-1]
# l.append(string)
l.append("]")
# l.append(string)
l.append("}")
return ''.join(l)
def pprint(self):
children = self.root.get_set()
return self.traverse(children[children.keys()[0]])
def getRoot(root_id, title_str, level):
tries = 0
while tries < 5:
try:
res = urllib.urlopen(base_url+str(root_id))
html = res.read()
soup = BeautifulSoup(html, 'xml')
title = soup.synset['words'].strip()
size = int(soup.synset['subtree_size'])
# ipdb.set_trace()
if len(title_str) != 0:
title = title_str + " > " +title
addToDatabase(title, size - 1)
for synset in soup.find_all('synset')[1:]:
getRoot(synset['synsetid'], title)
failed.remove(root_id)
except IOError:
time.sleep(1)
tries = tries + 1
failed.add(root_id)
continue;
except KeyError:
break
break
def addToDatabase(title, size):
post = {"name": title, "size": size}
db.tree.insert_one(post)
def createTree(tree, db):
cursor = db.tree.find({})
# count = 0
for document in cursor:
tree.insert(document['name'], document['size'])
def main():
base_url = 'http://imagenet.stanford.edu/python/tree.py/SubtreeXML?rootid='
client = MongoClient()
db = client.mydb
failed = set()
getRoot(82127, '')
tree = Tree()
createTree(tree, db)
file = open("testfile.txt", "w")
file.write(tree.pprint())
file.close()
if __name__ == '__main__':
main()