#Coding=utf-8ImportRequestsImportsysreload (SYS) sys.setdefaultencoding ('Utf-8')ImportRe,csvImportthreading,timeproxies= {'http':'Http://username:[email protected]:8080', 'HTTPS':'Https://username:[email protected]:8080'}deffuc (apikey): whileTrue:Try: With Lock:line=all_lines.pop (0)if( notLINE[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)ifInt (status[0])inch[0,1,2]: LNG= Re.findall ('"LNG":(. *),', R, re. S) Lat= Re.findall ('"LAT":(. *)}', R, re. S)iflng:line[34] =Lng[0] line[35] =Lat[0] Writer.writerow (line) Csvfile.flush ()Else: PrintLine[0]PrintR Writer.writerow (line) Csvfile.flush ()Else: PrintLine[0]PrintR Writer.writerow (line) Csvfile.flush () Break Else: Writer.writerow (line) Csvfile.flush ()exceptIndexerror:#Pop finished, process ended Break exceptException as E:PrintePrint 'End Time:'+time.strftime ('%y%m%d_%h%m%s', Time.localtime ())Print 'program start time:'+ 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 () forIinchAklines]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) forJinchReadersz: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 () forKeyinchAklines:thread= Threading. Thread (TARGET=FUC, args=(key,)) Thread.Start () threads.append (thread) time.sleep (1) forIinchThreads:i.join () csvfile.close () Fsz.close ( )Print 'program End time:'+time.strftime ('%y%m%d_%h%m%s', Time.localtime ())#fbj.close ()
Reference:
Http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
Get Baidu API coordinates