最近工作需要,写了一个下面脚本,作为备份
# -*- coding: cp936 -*-
#!/usr/bin/python
from struct import pack, unpack
from httplib import HTTP
import urllib2,json,fileinput,Queue,threading,time
#组件信息
proxy_handler = urllib2.ProxyHandler({'http': 'http://127.0.0.1:8080/'})
urlopener = urllib2.build_opener(urllib2.HTTPCookieProcessor())#,proxy_handler)
#http头
headers={'User-Agent':'Dalvik/1.6.0 (Linux; U; Android 4.0.4; ZTE N881F MIUI/3.7.7)'}
url=r'http://maps.google.com/maps/api/geocode/json?sensor=false&language=zh-cn&latlng=%s,%s'
def geturlcon(url,data=None,headers=headers,byte=None):
request = urllib2.Request(url,data,headers)
url = urlopener.open(request)
page=url.read(byte).decode('utf8','ignore')
return page
def fetch_latlong(query):
http = HTTP('mobilemaps.clients.google.com', 80)
http.putrequest('POST', '/glm/mmap')
http.putheader('Content-Type', 'application/binary')
http.putheader('Content-Length', str(len(query)))
http.endheaders()
http.send(query)
code, msg, headers = http.getreply()
result = http.file.read()
return result
def fetch_latlong_urllib(query):
headers = { 'User-Agent' : user_agent }
req = urllib2.Request(mmap_url, query, headers)
resp = urllib2.urlopen(req)
response = resp.read()
return response
def get_add_info(latitude,longitude):
d=geturlcon(url%(latitude,longitude))
a=json.loads(d)
#pprint.pprint(a['results'])
return a['results'][0]['formatted_address']+u'附近'
def get_location_by_cell(loc,cid):
size=3
if cid>65536:size=5
b_string = pack('>h14ss3ss4s3sb2si2sHi4s4sI',
0xe,
'\x00',
'\x1b',
'\x00',
'\x01',
'\x00\x00\x01\xcc',
'\x00',
size, #5表示大cid wcdma 3表示小cid gsm
'\x00',
cid,
'\x00',
loc,
1,
'\x00\x00\x01\xcc',
'\xff\xff\xff\xff',0)
bytes = fetch_latlong(b_string)
print len(bytes),list(bytes)
(a, b,errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",bytes)
latitude = latitude / 1000000.0
longitude = longitude / 1000000.0
return latitude,longitude
def doit1(loc,cid):
lat,lon=get_location_by_cell(loc,cid)
addr=get_add_info(lat,lon)
print lat,lon,addr
def doit(q,begin=1,stop=65536):
while True:
time.sleep(0.1)
if q.empty():break
loc=q.get()
f=open(str(loc)+'.txt','a')
f1=open('./err/'+str(loc)+'_err.txt','a')
for cid in xrange(begin,stop):
try:
lat,lon=get_location_by_cell(loc,cid)
addr=get_add_info(lat,lon)
print loc,cid,lat,lon,addr
print >>f,loc,cid,lat,lon,addr.encode('utf8')
f.flush()
except:
print loc,cid,'error'
print >>f1,loc,cid
f1.flush()
f1.close()
f.close()
if __name__ == '__main__':
doit1(28692, 20404)
doit1(28692 ,20405)
doit1(28692, 20406)
'''
q=Queue.Queue()
for i in xrange(50):
threading.Thread(target=doit,args=(q,)).start()
for i in fileinput.FileInput('loc.txt'):
q.put(int(i.strip()))
while True:
time.sleep(10)
if q.empty():break
'''