基于python的Dwnload地图图像
我正在尝试使用urllib模块下载python中的地图图像。但它总是失败基于python的Dwnload地图图像,python,google-maps,parsing,urllib,Python,Google Maps,Parsing,Urllib,我正在尝试使用urllib模块下载python中的地图图像。但它总是失败 我尝试将urllib.urlopen()与一些参数变量一起使用 在urllib.urlretrieve()中尝试 但是它不工作。而且,当我看到图像url的源代码时,我没有找到图像文件。以下是图片: 源代码: #-------------------------- PARSE IP ADDRESS ------------------------------- import re import urllib try
- 我尝试将
与一些参数变量一起使用urllib.urlopen()
- 在
urllib.urlretrieve()中尝试
而且,当我看到图像url的源代码时,我没有找到图像文件。以下是图片: 源代码:
#-------------------------- 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 IS 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 for this 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 And SAVE IT ------------------------
#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]
i = open("pict.png", "w")
i.write(get_map_img.read())
i.close()
print "End of file"
为什么要解析地图URL?自己建造:
import json, urllib
query = '' # IP to get coordinates of, leave empty for current IP
geo = urllib.urlopen('http://ip-api.com/json/%s?fields=240' % query)
result = json.load(geo)
if result['zip']:
zoom = 13
elif result['city']:
zoom = 12
else:
zoom = 6
map_img_url = "https://maps.googleapis.com/maps/api/staticmap?center=%s,%s&zoom=%i&size=320x385&sensor=false" % (result['lat'], result['lon'], zoom)
get_map_img = urllib.urlretrieve(map_img_url, "staticmap.png")
为什么要解析地图URL?自己建造:
import json, urllib
query = '' # IP to get coordinates of, leave empty for current IP
geo = urllib.urlopen('http://ip-api.com/json/%s?fields=240' % query)
result = json.load(geo)
if result['zip']:
zoom = 13
elif result['city']:
zoom = 12
else:
zoom = 6
map_img_url = "https://maps.googleapis.com/maps/api/staticmap?center=%s,%s&zoom=%i&size=320x385&sensor=false" % (result['lat'], result['lon'], zoom)
get_map_img = urllib.urlretrieve(map_img_url, "staticmap.png")
无法下载imagePaste full StackTrace无法下载imagePaste full StackTrace这对我来说非常有效。你知道是否/如何从谷歌地图上以类似的方式下载鸟瞰图吗?这对我来说非常有效。您是否知道/如何以类似的方式从谷歌地图下载鸟瞰图?