腾讯街景数据爬虫

目前腾讯为大家提供了海量的街景数据,并对其服务接口做出了详细的说明(https://lbs.qq.com/uri_v1/guide-showPano.html)。

  需要注意的是这里的referer需改为key,至于后边key对应的值需自己注册自己应用的key值。地址:https://lbs.qq.com/dev/console/key/manage

 

 

请求连接:

https://apis.map.qq.com/ws/streetview/v1/image?size=640x480&pano=10141050150725145721000&heading=0&pitch=0&key=K76BZ-W3O2Q-RFL5S-GXOPR-3ARIT-6KFE5

浏览器请求结果:

直接输入连接则会失败,需设置请求头。

失败效果图:

成功效果图:(设置Referer)

Python源代码

说明:

  本次实验主要需对武汉、北京等地区的街景数据爬虫,采用的核心方法如下:

  1. 采用市区最小外包矩形坐标限定拾取街景范围;
  2. 坐标采用wgs84转高德火星坐标的方式,坐标千分位依次递增1的方式逐点查询街景图片ID;
  3. 根据街景ID获取图片并保存;

  本文并未进行断点续爬以及相同街景去重操作,后续将完善;

  腾讯该接口并不稳定,维护时间距今较长,服务调用不易成功不建议使用该服务;

# coding=utf-8
import math
import requests
import urllib
from urllib.request import urlopen
import threading
from optparse import OptionParser
import cv2
try:
    import urlparse
except ImportError:
    import urllib.parse as urlparse
import numpy as np


#发送请求保存照片
def download(url, name):
    # url='https://apis.map.qq.com/ws/streetview/v1/image?size=640x480&pano=10141031130101141134000&heading=0&pitch=0&key=K76BZ-W3O2Q-RFL5S-GXOPR-3ARIT-6KFE5'
    # 将user_agent,referer写入头信息
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36','Referer':'https://lbs.qq.com/tool/streetview/streetview.html'}
    images = requests.get(url, headers=headers)
    img = images.content
    if images.status_code == 200:
        print('图片: %s%s 正在下载..' % ('张飒','xin'))
        with open(name,'wb') as fp:
            fp.write(img)
# wgs84转高德
def wgs84togcj02(lng, lat):
    PI = 3.1415926535897932384626
    ee = 0.00669342162296594323
    a = 6378245.0
    dlat = transformlat(lng - 105.0, lat - 35.0)
    dlng = transformlng(lng - 105.0, lat - 35.0)
    radlat = lat / 180.0 * PI
    magic = math.sin(radlat)
    magic = 1 - ee * magic * magic
    sqrtmagic = math.sqrt(magic)
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI)
    dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * PI)
    mglat = lat + dlat
    mglng = lng + dlng
    return [mglng, mglat]
# GCJ02/谷歌、高德 转换为 WGS84 gcj02towgs84
def gcj02towgs84(localStr):
    lng = float(localStr.split(',')[0])
    lat = float(localStr.split(',')[1])
    PI = 3.1415926535897932384626
    ee = 0.00669342162296594323
    a = 6378245.0
    dlat = transformlat(lng - 105.0, lat - 35.0)
    dlng = transformlng(lng - 105.0, lat - 35.0)
    radlat = lat / 180.0 * PI
    magic = math.sin(radlat)
    magic = 1 - ee * magic * magic
    sqrtmagic = math.sqrt(magic)
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI)
    dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * PI)
    mglat = lat + dlat
    mglng = lng + dlng
    return str(lng * 2 - mglng) + ',' + str(lat * 2 - mglat)
def transformlat(lng, lat):
    PI = 3.1415926535897932384626
    ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * \
          lat + 0.1 * lng * lat + 0.2 * math.sqrt(abs(lng))
    ret += (20.0 * math.sin(6.0 * lng * PI) + 20.0 *
            math.sin(2.0 * lng * PI)) * 2.0 / 3.0
    ret += (20.0 * math.sin(lat * PI) + 40.0 *
            math.sin(lat / 3.0 * PI)) * 2.0 / 3.0
    ret += (160.0 * math.sin(lat / 12.0 * PI) + 320 *
            math.sin(lat * PI / 30.0)) * 2.0 / 3.0
    return ret
def transformlng(lng, lat):
    PI = 3.1415926535897932384626
    ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + \
          0.1 * lng * lat + 0.1 * math.sqrt(abs(lng))
    ret += (20.0 * math.sin(6.0 * lng * PI) + 20.0 *
            math.sin(2.0 * lng * PI)) * 2.0 / 3.0
    ret += (20.0 * math.sin(lng * PI) + 40.0 *
            math.sin(lng / 3.0 * PI)) * 2.0 / 3.0
    ret += (150.0 * math.sin(lng / 12.0 * PI) + 300.0 *
            math.sin(lng / 30.0 * PI)) * 2.0 / 3.0
    return ret

#获取经纬坐标
def getPoint(_points):
    point = _points.split(',')
    point_jin = point[0]
    point_wei = point[1]
    transOpints=wgs84togcj02(float(point_jin),float(point_wei))
    return transOpints

# 输入左下以及右上角坐标 根据两点形成等差坐标组 进而获取图片
def getImage(start_point,end_point,cityName):
    # 取得起始坐标
    start_point_jin = start_point[0]
    start_point_wei = start_point[1]
    end_point_jin = end_point[0]
    end_point_wei = end_point[1]
    #创建等差数组
    jins = np.arange(float(start_point_jin)*1000, float(end_point_jin)*1000, 1)*0.001
    jins_num = len(jins)
    weis = np.linspace(float(start_point_wei)*1000, float(end_point_wei)*1000, jins_num)*0.001
    weis_num = len(weis)
    for jins_i in range(jins_num):
        jin = jins[jins_i]
        for weis_i in range(weis_num):
            wei = weis[weis_i]
            #这里要注意下,对应的经纬度没有街景图的地方,输出的会是无效图片
            print(jin, wei)
            img_name = "E:\\dataTest\\streetImgData\\"+cityName+"\\" + str(wei) + "_" + str(jin) +".jpg"
            url = "https://apis.map.qq.com/ws/streetview/v1/image?size=600x480&location="+str(wei)+","+str(jin)+"&pitch=0&heading=0&key=E2BBZ-AEB6U-ONRVX-4PBS3-CZIHK-A7FJI"
            outimg = download(url, img_name)

#定义数据字典 根据起始点坐标推算内容坐标
cityJinweiArr=[{"start":"115.442845,39.464988","end":"117.498766,40.978318","city":"beiJing"},{"start":"112.681398,34.269097","end":"114.226897,34.958295","city":"zhengZhou"},{"start":"113.692462,29.971956","end":"115.082138,31.362241","city":"wuHan"}]
for city in cityJinweiArr:
    start_point=getPoint(city['start'])
    end_point=getPoint(city['end'])
    cityName=city['city']
    getImage(start_point,end_point,cityName)

 

posted @ 2019-12-04 13:54  红领巾1994  阅读(2772)  评论(10编辑  收藏  举报