最近有个需求是要爬取街景图片,国内厂商百度高德和腾讯地图都没有开放接口,查询资料得知谷歌地图开放街景api
谷歌捷径申请key地址:https://developers.google.com/maps/documentation/streetview/
下面是一些街景照片的参数信息。
size:图片尺寸
location:经纬度
heading:朝向角度
pitch:旋转角度
key:自己的API_KEY
下面开始代码:
1 # _*_ coding: utf-8 _*_ 2 __author__ = \'herrylee\' 3 4 import urllib.request 5 6 def download(url, name): 8 #url = "http://pic2.sc.chinaz.com/files/pic/pic9/201309/apic520.jpg" 9 #保存文件时候注意类型要匹配,如要保存的图片为jpg,则打开的文件的名称必须是jpg格式,否则会产生无效图片 10 conn = urllib.request.urlopen(url) 11 12 f = open(name, \'wb\') #wb以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 13 f.write(conn.read()) 14 f.close() 15 print(\'Pic Saved!\') 16 17 fp = open("C:\\Users\\Administrator\\Desktop\\GPS\\paris.txt","r") #以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。 18 for line in fp.readlines(): 19 line = (lambda x: x[11:33])(line) #选取从第十一个到第十三个字符 也可以写成line = (lambda x: x[11:-11])(line) 20 print(line) 21 zu = line.split(\'_\') 22 jin = zu[0] 23 wei = zu[1] 24 heading = zu[2] 25 name = "C:\\Users\\Administrator\\Desktop\\GPS\\paris_sub\\" + jin + "_" + wei + "_" + heading + "_-004.JPG" 26 url = "https://maps.googleapis.com/maps/api/streetview?size=936x537&location=" + jin + "," + wei + "&heading=" +heading + "&pitch=-004&key="+"key" 27 print(name) 28 print (url) 29 download(url, name) 30 fp.close()
下面是读取的坐标信息txt文档内容
\'paris_sub/48.651717_2.493865_270_-004.JPG\'
\'paris_sub/48.756312_2.069988_90_-004.JPG\'
\'paris_sub/48.759815_2.502092_90_-004.JPG\'
\'paris_sub/48.911445_1.850626_270_-004.JPG\'
\'paris_sub/48.893319_2.262638_90_-004.JPG\'
\'paris_sub/48.815737_2.412183_270_-004.JPG\'
\'paris_sub/48.905476_2.527302_90_-004.JPG\'
\'paris_sub/48.912099_2.285934_270_-004.JPG\'
\'paris_sub/48.772068_2.033889_90_-004.JPG\'
\'paris_sub/48.648679_2.306182_90_-004.JPG\'
\'paris_sub/48.618283_2.925704_90_-004.JPG\'
\'paris_sub/48.926558_1.941170_270_-004.JPG\'
\'paris_sub/48.881077_2.705896_90_-004.JPG\'
\'paris_sub/48.966915_2.477976_90_-004.JPG\'
\'paris_sub/49.105046_2.245066_270_-004.JPG\'
\'paris_sub/48.703037_2.216347_270_-004.JPG\'
\'paris_sub/48.791862_2.420343_90_-004.JPG\'
\'paris_sub/48.681379_2.660818_90_-004.JPG\'
\'paris_sub/48.712748_2.377744_90_-004.JPG\'
\'paris_sub/48.806908_2.604621_270_-004.JPG\'
\'paris_sub/48.652523_1.862794_90_-004.JPG\'
\'paris_sub/48.488890_2.270909_90_-004.JPG\'
\'paris_sub/48.525296_1.950706_270_-004.JPG\'
运行脚本输出信息
有一点需要注意 由于爬取的谷歌街景,众所周知的原因,我们需要fq,因此采取的模式是ssr,代理模式应该是全局模式,否则会报错 访问不通。