-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgetLocation.py
45 lines (33 loc) · 1.13 KB
/
getLocation.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
# -*- coding: UTF-8 -*-
import urllib2,json
import MySQLdb
import spider
def getLocation(address):
url = 'http://api.map.baidu.com/geocoder/v2/'
output = 'json'
ak = 'hpX9BN94G7wEQDOvrdjoDSaKoMN8eiBR'
uri = url + '?' + 'address=' + address + '&output=' + output + '&ak=' + ak
temp = urllib2.urlopen(uri)
temp = json.loads(temp.read())
if temp['status'] == 0:
return temp
return False
def insertLocationToDatabase():
names = spider.returnHospisalName()
db = MySQLdb.connect("localhost","root","","putianhospital",charset='utf8' )
for name in names:
address_json = getLocation(name)
if not address_json:
print(name+"GET LOCATION FAILED")
continue
lng = (address_json['result']['location']['lng'])
lat = (address_json['result']['location']['lat'])
cursor = db.cursor()
sql = 'INSERT INTO hospitalLocation VALUES("%s","%s","%s");' %(name,lng,lat)
try:
cursor.execute(sql)
except:
print(name+"INSERT FAILED")
db.close()
if __name__ == '__main__':
insertLocationToDatabase()