-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebscrap.py
262 lines (230 loc) · 9.33 KB
/
webscrap.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import requests
import re
from requests import *
import xml.etree.ElementTree as ET
import re
import json
f = open( "D:\enitity\ids_singapore.txt", "r" )
a = []
for line in f:
a.append(line)
#fwrite = open('output_sg_geo', 'w')
#c = 0
'''
master_data = {
"image_url": "",
"name": "",
}
'''
for i in a:
url1= "http://multicodes.giatamedia.com/webservice/rest/1.0/properties/" + re.sub('[^0-9]',"",i)
url2="http://ghgml.giatamedia.com/webservice/rest/1.0/items/"+re.sub('[^0-9]',"",i)
url3="http://ghgml.giatamedia.com/webservice/rest/1.0/factsheets/"+re.sub('[^0-9]',"",i)
url4="http://ghgml.giatamedia.com/webservice/rest/1.0/texts/en/"+re.sub('[^0-9]',"",i)
id=str(i)
#url = 'http://multicodes.giatamedia.com/webservice/rest/1.0/properties/p'
username = 'bhaskar|crayondata.com'
password = 'hkUrJ2oo'
l_1=requests.get(url1, auth=(username, password))
l_2=requests.get(url2, auth=(username, password))
l_3=requests.get(url3, auth=(username, password))
l_4=requests.get(url4, auth=(username, password))
#print l_1
#print l_2
#print l_3
#print l_4
#import pdb;pdb.set_trace()
root1 = ET.fromstring((l_1.text.encode("utf-8")))
root1_a=root1[0]
first= root1.tag
root2 = ET.fromstring((l_2.text.encode("utf-8")))
second= root2.tag
root2_a=root2[0]
#print root2_a
#print root2_a.tag
root3 = ET.fromstring((l_3.text.encode("utf-8")))
root3_a=root3[0]
third= root3.tag
if third != "error":
#import pdb;pdb.set_trace()
root4 = ET.fromstring(l_4.text.encode("utf-8"))
root4_a=root4[0]
fourth= root4_a
if fourth == "error":
#print root3_a[4][0]
try:
for section in root3_a[4][0]:
fact_master_dict[section.attrib["name"]] = {}
for fact in section[0]:
fact_attrib = fact.attrib
fact_master_dict[section.attrib["name"]][fact_attrib["name"]] = fact[0].text
except:
fact_master_dict = {}
#import pdb;pdb.set_trace()
#print fact_master_dict
fact_master_dict2 = {}
try:
ad = [add.text for add in root1_a.find("addresses").find("address").iter("addressLine")]
address = ", ".join(ad)
except:
address={}
try:
latitude=root1_a.find("geoCodes").find("geoCode").find("latitude").text
except:
latitude={}
try:
longitude=root1_a.find("geoCodes").find("geoCode").find("longitude").text
except:
longitude={}
try:
name=root1_a.find("name").text
except:
name={}
phone_dict={}
try:
for phone in root1_a.find("phones"):
phone_attrib=phone.attrib
phone_dict[phone_attrib["tech"]]=phone.text
except:
phone_dict={}
image=[]
try:
images=root2_a.find("images")
for im in images.findall("image"):
typ=im.get("type")
image.append(typ)
imag=",".join(image)
except:
imag={}
master_dict = {"id": id,"name": name,"address": address,"latitude": latitude,"longitude": longitude,"image": imag,"contact": phone_dict,"factsheet": fact_master_dict,"details": fact_master_dict2}
cont=json.dumps(master_dict)
print(master_dict['name']+" "+master_dict['address']+" "+master_dict['latitude']+" "+master_dict['longitude'])
#print(l.content)
#fwrite.write(cont+ "\n")
else:
#print root4_a[0][0][0]
#print root4_a[0]
#print root4_a[0][0][0]
fact_master_dict = {}
#print root3_a[4][0]
try:
for section in root3_a[4][0]:
fact_master_dict[section.attrib["name"]] = {}
for fact in section[0]:
fact_attrib = fact.attrib
fact_master_dict[section.attrib["name"]][fact_attrib["name"]] = fact[0].text
except:
fact_master_dict = {}
#import pdb;pdb.set_trace()
#print fact_master_dict
fact_master_dict2 = {}
try:
for section in root4_a[0][0][0]:
fact_master_dict2[section[0].text]= section[1].text
#print fact_master_dict2
#for title in section:
#print title
#fact_master_dict2[section[0].text] = {}
#tit=section.text
except:
fact_master_dict2 = {}
try:
ad = [add.text for add in root1_a.find("addresses").find("address").iter("addressLine")]
address = ", ".join(ad)
except:
address={}
try:
latitude=root1_a.find("geoCodes").find("geoCode").find("latitude").text
except:
latitude={}
try:
longitude=root1_a.find("geoCodes").find("geoCode").find("longitude").text
except:
longitude={}
try:
name=root1_a.find("name").text
except:
name={}
phone_dict={}
try:
for phone in root1_a.find("phones"):
phone_attrib=phone.attrib
phone_dict[phone_attrib["tech"]]=phone.text
except:
phone_dict={}
image=[]
try:
images=root2_a.find("images")
for im in images.findall("image"):
typ=im.get("type")
image.append(typ)
imag=",".join(image)
except:
imag={}
master_dict = {"id": id,"name": name,"address": address,"latitude": latitude,"longitude": longitude,"image": imag,"contact": phone_dict,"factsheet": fact_master_dict,"details": fact_master_dict2}
cont=json.dumps(master_dict)
print(master_dict['name']+" "+master_dict['address']+" "+master_dict['latitude']+" "+master_dict['longitude'])
#print(l.content)
#fwrite.write(cont+ "\n")
else:
#import pdb;pdb.set_trace()
root4 = ET.fromstring(l_4.text.encode("utf-8"))
root4_a=root4[0]
fourth= root4_a
if fourth == "error":
fact_master_dict2 = {}
else:
#print root4_a[0][0][0]
#print root4_a[0]
#print root4_a[0][0][0]
fact_master_dict = {}
#print root3_a[4][0]
#import pdb;pdb.set_trace()
#print fact_master_dict
fact_master_dict2 = {}
try:
for section in root4_a[0][0][0]:
fact_master_dict2[section[0].text]= section[1].text
#print fact_master_dict2
#for title in section:
#print title
#fact_master_dict2[section[0].text] = {}
#tit=section.text
except:
fact_master_dict2 = {}
try:
ad = [add.text for add in root1_a.find("addresses").find("address").iter("addressLine")]
address = ", ".join(ad)
except:
address={}
try:
latitude=root1_a.find("geoCodes").find("geoCode").find("latitude").text
except:
latitude={}
try:
longitude=root1_a.find("geoCodes").find("geoCode").find("longitude").text
except:
longitude={}
try:
name=root1_a.find("name").text
except:
name={}
phone_dict={}
try:
for phone in root1_a.find("phones"):
phone_attrib=phone.attrib
phone_dict[phone_attrib["tech"]]=phone.text
except:
phone_dict={}
image=[]
try:
images=root2_a.find("images")
for im in images.findall("image"):
typ=im.get("type")
image.append(typ)
imag=",".join(image)
except:
imag={}
master_dict = {"id": id,"name": name,"address": address,"latitude": latitude,"longitude": longitude,"image": imag,"contact": phone_dict,"factsheet": fact_master_dict,"details": fact_master_dict2}
cont=json.dumps(master_dict)
print(master_dict['name']+" "+master_dict['address']+" "+master_dict['latitude']+" "+master_dict['longitude'])