标签:.com str requests username dup span 结束时间 key page
#coding=utf-8 import requests import sys reload(sys) sys.setdefaultencoding(‘utf-8‘) import re,csv import threading,time proxies = {‘http‘: ‘http://username:password@proxyhk.huawei.com:8080‘, ‘https‘: ‘https://username:password@proxyhk.huawei.com:8080‘} def fuc(apikey): while True: try: with lock: line = all_lines.pop(0) if (not line[34]) or (line[34] == ‘0‘): addr = (line[10]+line[12]+line[14]).replace(‘#‘,‘‘).decode(‘utf-8‘)[-42:].encode(‘utf-8‘) # addr_str = addr.decode(‘utf-8‘) # addr = addr_str[-100:]#地址过长搜索不到 url = ‘http://api.map.baidu.com/geocoder/v2/?address=%s&output=json&ak=%s&callback=showLocation‘ % (addr, apikey) r = requests.get(url=url, proxies=proxies).content status = re.findall(‘"status":(.*?),‘, r, re.S) if int(status[0]) in [0,1,2]: lng = re.findall(‘"lng":(.*?),‘, r, re.S) lat = re.findall(‘"lat":(.*?)}‘, r, re.S) if lng: line[34] = lng[0] line[35] = lat[0] writer.writerow(line) csvFile.flush() else: print line[0] print r writer.writerow(line) csvFile.flush() else: print line[0] print r writer.writerow(line) csvFile.flush() break else: writer.writerow(line) csvFile.flush() except IndexError:#pop完,进程结束 break except Exception as e: print e print ‘结束时间:‘+time.strftime(‘%Y%m%d_%H%M%S‘,time.localtime()) print ‘程序开始时间:‘ + time.strftime(‘%Y%m%d_%H%M%S‘, time.localtime()) with open(u‘D:\\python_project\\yellowPageGeo\\api.txt‘, ‘rb‘) as baiduak: akLines = baiduak.readlines() akLines = [i.strip() for i in akLines] csvFile=open(u‘D:\\python_project\\yellowPageGeo\\result\\Final_Data_sz_nullResult.csv‘,‘wb‘) writer=csv.writer(csvFile) all_lines = [] fsz=open(u‘D:\\python_project\\yellowPageGeo\\Final_Data_sz_null.csv‘,‘rb‘) readersz=csv.reader(fsz) for j in readersz: all_lines.append(j) # fbj=open(u‘C:\\Users\\lwx351192\\Desktop\\Final_Data_bj_dedup1.csv‘,‘rb‘) # readerbj=csv.reader(fbj) # for k in readerbj: # all_lines.append(k) threads=[] lock=threading.Lock() for key in akLines: thread = threading.Thread(target=fuc, args=(key,)) thread.start() threads.append(thread) time.sleep(1) for i in threads:i.join() csvFile.close() fsz.close() print ‘程序结束时间:‘+time.strftime(‘%Y%m%d_%H%M%S‘,time.localtime()) # fbj.close()
参考:
http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
标签:.com str requests username dup span 结束时间 key page
原文地址:http://www.cnblogs.com/BigFishFly/p/6337187.html