Dwnload map image with python -
i trying download map image in python urllib module. failed.
- i'm tried use
urllib.urlopen()
parameter variants - tried in
urllib.urlretrieve()
but doesn't work.
and, when see source code of image url, didn't find image file. here image: https://maps.googleapis.com/maps/api/staticmap?center=31.0456,121.3997&zoom=12&size=320x385&sensor=false
source code:
#-------------------------- parse ip address ------------------------------- import re import urllib try: mysite = urllib.urlopen('http://ip-api.com/line') except urllib.httperror, e: print "cannot retrieve url: http error code", e.code except urllib.urlerror, e: print "cannot retrieve url: " + e.reason[1] list_of_params = mysite.read() print list_of_params ip_arr = list_of_params.splitlines() #--------------------- here find map image -------------------------------------- try: map_page = urllib.urlopen('http://ip-api.com') except urllib.httperror, e: print "cannot retrieve url: http error code", e.code except urllib.urlerror, e: print "cannot retrieve url: " + e.reason[1] #f = open("data.html", "w") #f.write(str(mysite.read())) #f.close() #looking in page pattern = re.findall(re.compile("url\(\'(https://maps\.googleapis\.com/maps/api/staticmap\?center=.*)\'"), page_get_map.read()) map_img_url = pattern[0].replace('&', '&') #------------------- download map image , save ------------------------ #file_name = map_img_url.rsplit('/',1)[1] try: get_map_img = urllib.urlretrieve(map_img_url, "staticmap.png") except urllib.httperror, e: print "cannot retrieve url: http error code", e.code except urllib.urlerror, e: print "cannot retrieve url: " + e.reason[1] = open("pict.png", "w") i.write(get_map_img.read()) i.close() print "end of file"
import requests f=open('static.png','wb') f.write(requests.get('https://maps.googleapis.com/maps/api/staticmap?center=31.0456,121.3997&zoom=12&size=320x385&sensor=false').content) f.close()
Comments
Post a Comment