資源簡介
批量將地址解析為百度地圖經緯度坐標,需要自己申請AK,可以指定并發數量

代碼片段和文件信息
#coding=utf-8
import?pymongo
from?gevent.pool?import?Pool
import?urllib.request
import?json
import?urllib.parse
import?socket
from?gevent?import?monkey
from?parseSettings?import?*
import?gevent
import?time
import?datetime
monkey.patch_all() #修改標準庫
socket.setdefaulttimeout(2)?
class?LocationParser(object):
#用于地址解析的類
def?__init__(selfakbaseUrl):
self.ak?=?ak
self.baseUrl?=?baseUrl
def?parse(selfaddressoutputcity=None):
#通過api獲取json數據
url?=?self.baseUrl+“?ak=“+self.ak+“&address=“+urllib.parse.quote(address)+“&output=“+output
if?city:
url?=?url?+?“&city=“+urllib.parse.quote(city)
try:
result?=?urllib.request.urlopen(url)
except:
print(“連接錯誤“)
return?Noneurl
jsonresult?=?json.load(result)
return?jsonresulturl
class?LocationDumper():
def?__init__(selfipportlocationDBlocationColsourceDBsourceColrequestNum?=?10city?=?None):
#數據庫連接
self.client?=?pymongo.MongoClient(ipport)
#地址解析器
self.bParser?=?LocationParser(akbaseUrl);
#并發數量控制
self.requestNum?=?requestNum
#帶解析數據集合
self.sourceCol?=?self.client[sourceDB][sourceCol]
#用于存儲結果的集合
self.locationCol?=?self.client[locationDB][locationCol]
self.oklength?=?0
self.errorlength?=?0
#用于解析每一條數據的方法
def?parseData(selfdata):
#數據集中沒有帶解析的數據解析該條數據
if?self.locationCol.count({“_id“:data[“_id“]})?<=?0:
if?self.locationCol.count({“address“:data[“address“]})?>?0:
print(“同一地址不再解析“+data[‘address‘])
content?=?self.locationCol.find_one({“address“:data[“address“]})
a?=?{‘_id‘:data[‘_id‘]
????????????????“size“:data[‘size‘]
????????????????“orient“:data[‘orient‘]
????????????????“roomNum“:data[‘roomNum‘]
????????????????“url“:data[‘fromUrl‘]
????????????????“unitPrice“:data[‘unitPrice‘]
????????????????“sumPrice“:data[‘sumPrice‘]
????????????????“ln“:content[‘ln‘]
????????????????“lat“:content[‘lat‘]
????????????????“address“:data[‘address‘]
????????????????“time“:data[‘nowTime‘]
????????????????“city“:data[‘city‘]}
self.locationCol.insert(a)
self.sourceCol.update({“_id“:data[“_id“]}{“$set“:{“status“:“OK“}})
return
result?=?self.bParser.parse(data[“address“]“json“data[“city“])
#根據返回碼判斷解析結果是否正確如果不正確去掉城市參數重試
if?result[0]?and?result[0][‘status‘]?!=?0:
result?=?self.bParser.parse(data[“address“]“json“)
if?not?result[0]:
return
jsonResult?=?result[0]
urlResult?=?result[1]
ln?=?None
lat?=?None
if?jsonResult[‘status‘]?==?0:
ln?=?jsonResult[‘result‘][‘location‘][‘lng‘]
lat?=?jsonResult[‘result‘][‘location‘][‘lat‘]
else:
try:
print(“地址解析錯誤status:“+str(jsonResult[‘status‘])+“msg:“+jsonResult[‘msg‘]+“errorUrl:“+urlResult)
self.sourceCol.update({“_id“:data[“_id“]}{“$set“:{“status“:“ERROR“}})
self.errorlength?+=?1
except:
print(“沒有錯誤信息直接輸出信息“+str(jsonResult))
return
try:
a?=?{‘_id‘:data[‘_id‘]
“size“:data[‘size‘]
“orient“:data[‘orient‘]
“roomNum“:data[‘roomNum‘]
“url“:data[‘fromUrl‘]
“uni
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-12-08?00:14??parseModule\
?????文件????????4793??2017-12-15?08:55??parseModule\parse.py
?????文件?????????104??2017-12-08?00:14??parseModule\parse.sh
?????文件?????????370??2017-12-15?08:55??parseModule\parseSettings.py
?????目錄???????????0??2017-12-08?00:14??parseModule\__pycache__\
?????文件?????????348??2017-12-08?00:14??parseModule\__pycache__\parseSettings.cpython-36.pyc
評論
共有 條評論