Google地图瓦片爬虫——进阶版

紧接上一篇------Google地图瓦片爬虫

clash节点自动切换

为了防止一个IP地址访问频率过快问题,自动切换clash的节点

python 复制代码
def change_node(is_stop):
    while True:
        _r = requests.get("http://127.0.0.1:11053/proxies", headers=clash_headers, verify=False)
        # 这里匹配Lv开头的节点
        proxy_list = [proxy for proxy in _r.json()["proxies"]["XXX"]["all"] if proxy.startswith("Lv")]

        proxy = random.choice(proxy_list)

        payload = json.dumps({"name": proxy})
		# XXX替换为自己的分组名称
        requests.put("http://127.0.0.1:11053/proxies/XXX", headers=clash_headers, data=payload, verify=False)

        time.sleep(5)
        if is_stop.value:
            print("Finished.")
            break

多进程爬虫

  • get_tile:用于爬取瓦片
  • write_to_db:用于写入数据库
  • change_node:用于切换clash节点
python 复制代码
def main():
    is_stop = multiprocessing.Value("I", 0)

    db_path, db_name, tile_list = init_db()
    total = len(tile_list)
    print(total)

    # 创建任务队列
    data_queue = Queue()

    process_list = []
    p_number = 10
    step = total // p_number + 1
    for i in range(p_number + 1):
        process_list.append(
            Process(target=get_tile, args=(data_queue, db_path, db_name, tile_list[i * step:(i + 1) * step],)))

    process_list.append(Process(target=write_to_db, args=(data_queue, db_path, total, is_stop)))

    process_list.append(Process(target=change_node, args=(is_stop,)))

    for p in process_list:
        p.start()

    for p in process_list:
        p.join()

完整程序

下载地址:https://download.csdn.net/download/this_is_id/90343579

相关推荐
x***J34815 小时前
Python多线程爬虫
开发语言·爬虫·python
m***D28615 小时前
Python网络爬虫实战案例
开发语言·爬虫·python
青青子衿_211 天前
TikTok爬取——视频、元数据、一级评论
爬虫·python·selenium
interception1 天前
爬虫js逆向,jsdom补环境,抖音,a_bogus
javascript·爬虫·python
q***2512 天前
Python中的简单爬虫
爬虫·python·信息可视化
Glommer2 天前
简单聊一下 tls 指纹校验
爬虫·浏览器
xinxinhenmeihao2 天前
爬虫为什么要用动态ip?动态IP在爬虫中起到哪些作用?
爬虫·网络协议·tcp/ip
APIshop2 天前
代码解析:通过第三方爬虫获取1688商品详情接口
爬虫·okhttp
深蓝电商API3 天前
初级爬虫反爬应对:解决 403、IP 限制的简单方法
爬虫·python
深蓝电商API3 天前
爬虫速度优化:初级阶段如何提升爬取效率(无复杂操作)
爬虫