1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| import requests as req import math from time import sleep from random import random import os
def main(): leftTopPoint = [52, 50] rightButtomPoint = [134, 12] zoom = 10 tileList = [] print("正在分析需要下载的瓦片") tileList = calcAllTile(leftTopPoint, rightButtomPoint, zoom) allCount = len(tileList) tileDownloadStatusList = [0] * allCount print("共计需要下载 {} 张瓦片,至少消耗约 {} Mb数据".format(allCount, allCount*10/1000))
baseUrl = 'https://mt.google.com/vt/lyrs=y&x={}&y={}&z={}&hl=en' downloadCount = 0 yiXiaZai = 0 for tileIndex in range(0,allCount): tMessage = tileList[tileIndex].split('/') z = tMessage[0] x = tMessage[1] y = tMessage[2] currentImgUrl = baseUrl.format(x,y,z) tileDownloadStatusList[tileIndex] = downloadImg(currentImgUrl, tileList[tileIndex]) yiXiaZai += 1 print("当前进度: {} / {}".format(yiXiaZai, allCount))
downloadCount += 1 if downloadCount > 500: print("正在sleep(60), 防止被ban...") sleep(60) downloadCount = 0
print('正在检查下载失败的图片:') faultImg = [] for i in range(0, len(tileDownloadStatusList)): if tileDownloadStatusList[i] == 0: faultImg.append(tileList[i]) print('下载失败的图片有:') print(faultImg)
def downloadImg(url, path): standardHeader = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Connection": "keep-alive", "Referer": "https://map.google.com/", "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0", } res = '' try: res = req.get(url, headers=standardHeader) except: return 0 if res.status_code == 200: f = open('./'+path+'.jpg', 'wb') f.write(res.content) f.close() print("{} 下载完成,请查看:{}".format(url, './'+path+'.jpg')) return 1 else: return 0
def calcAllTile(leftTopPoint, rightButtomPoint, zoom): xMinTile, yMinTile = convertCoordinatesToTile(leftTopPoint[0],leftTopPoint[1], zoom) xMaxTile, yMaxTile = convertCoordinatesToTile(rightButtomPoint[0],rightButtomPoint[1], zoom) tileList = [] base = str(zoom) + "/{}/{}" current_directory = os.getcwd() for xTile in range(xMinTile, xMaxTile+1): for yTile in range(yMinTile, yMaxTile+1): tileList.append(base.format(xTile,yTile)) directory = os.path.join(current_directory, base.format(xTile, '')) os.makedirs(directory, exist_ok=True) return tileList
def convertCoordinatesToTile(lon, lat, zoom): xtile = int(math.floor((lon + 180) / 360 * (1 << zoom))) ytile = int(math.floor((1 - math.log(math.tan(math.radians(lat)) + 1 / math.cos(math.radians(lat))) / math.pi) / 2 * (1 << zoom)))
if xtile < 0: xtile = 0 if xtile >= (1 << zoom): xtile = (1 << zoom) - 1 if ytile < 0: ytile = 0 if ytile >= (1 << zoom): ytile = (1 << zoom) - 1
return xtile, ytile
def bye(): print("\n\n同行你好,感谢使用我的脚本") print("如果你喜欢这个脚本,可以给个三连或者给\n 13598131990 打赏个午饭,谢谢") print("有问题也欢迎加我微信沟通") print("遥祝生活愉快,工作顺利")
def hello(): os.system("clear") bye() print("\n\n下载将在数秒后开始\n\n") sleep(2)
if __name__ == '__main__': hello() main() bye()
|