腾讯地图街景爬取

前面操作参考上一篇博客

腾讯地图API爬取街景

1用到工具将经纬度转换成可识别的场景id 坐标转成panoid,执行read.html,将执行好的panoid先复制在word,然后转换成txt,最后将这一列添加到excel

转换工具主要代码





  
  
  
  
  转换



  

读取excel(仅读取第一个sheet)

pano:

结果输出:(下面表格可直接编辑导出)

A B C D
1 name 经度 纬度
2 人民中路一段 104.0672425 30.65643045

导出excel

上面读取的表格您可以直接编辑,编辑后点击保存即可导出excel文件。

2 然后在streetview下新建2个文件夹,raw_img 和meg_img 两个文件夹,并将要读取的excel转换成csv格式,放在/streetview目录下

3 执行s_2_get_and_merge_img.py文件就可以 不受key限制爬取街景

# -*- coding: utf-8 -*-

# Spider Lv4
# Author: Yue H.W. Luo
# Mail: [email protected]
# License : http://www.apache.org/licenses/LICENSE-2.0
# More detial: https://blog.rimoe.xyz/2019/03/14/post01/

"""
## NOTE
   Created on Wed Mar 13 21:24:23 2019
   This programme is used to get data from Tencent street view.
   Get the image of a street view point.

"""

import os
import cv2
import json
import requests
import numpy as np

os.chdir(r'street')
rpath = 'raw_img'
mpath = 'meg_img'


def download(path, svid, x, y):
    # down street view image
    url = 'https://sv6.map.qq.com/tile?svid={}&x={}&y={}&level=0' +\
      '&mtype=mobile-cube&from=web'
    path = '{}/id{}_x{}_y{}.jpg'.format(path, svid, x, y)
    r = requests.get(url.format(svid, x, y))
    with open(path, 'wb') as fd:
        fd.write(r.content)


def merge_y(path, svid, x):
    # merge y axis
    img1 = cv2.imread('%s/id%s_x%s_y0.jpg' % (path, svid, x), 1)
    img2 = cv2.imread('%s/id%s_x%s_y1.jpg' % (path, svid, x), 1)
    if img1 is None:
        download(path, svid, x, 0)
    if img2 is None:
        download(path, svid, x, 1)
    try:
        return np.concatenate([img1, img2], axis=0)
    except ValueError:
        return merge_y(path, svid, x)


def merge_x(path, svid, n):
    # merge x axis
    x = 2 * n
    img1 = merge_y(path, svid, x)
    img2 = merge_y(path, svid, x + 1)
    return np.concatenate([img1, img2], axis=1)


# read todo list
with open('./TwoRingRoad_火星坐标_9058panoid.csv', 'r') as rf:
    rf.readline()
    clean = rf.readlines()

done = []
# with open('done_meg.json', 'r') as wf:
#    done = json.load(wf)

for svid in clean:
    info = svid.split(',')
    svid = svid.split(',')[0]
    if svid in done:
        continue
    for n in range(4):
        _img = merge_x(rpath, svid, n)
        # cv2.imwrite('{}/{}_{}_{}_0_{}.jpg'.format(mpath, info[1], info[2], info[3].replace('\n', ''), n*90), _img)
        cv2.imencode('.jpg', _img)[1].tofile('{}/{}_{}_{}_0_{}.jpg'.format(mpath, info[1], info[2], info[3].replace('\n', ''), n*90))
    done.append(svid)
    # save for interup
    print ("done:%s_%s" % (svid, len(done)))
    with open('done_meg.json', 'w') as wf:
        json.dump(done, wf)

 

你可能感兴趣的:(python爬虫)