爬取广州所有停车场数据(Python)(并行加速版本)
生活随笔
收集整理的這篇文章主要介紹了
爬取广州所有停车场数据(Python)(并行加速版本)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前做過這個的單進程版本。這次使用多進程來實現~
爬取廣州所有停車場數據(Python)
大家可以對比著看一下,在原來的單進程爬蟲的基礎進行改進而得到的產品。
import requests import os import json import multiprocessing import timedef getAllJson(Session, data, headers):url = 'http://219.136.133.163:8000/Pages/Commonpage/AsyGetData.asmx/GetParkList'res = Session.post(url, data=data, headers=headers)res.encoding = 'utf-8'with open('./Data/' + data['cp'] + '.json', 'w') as f:json.dump(res.json(), f, ensure_ascii=False)if __name__ == '__main__':st = time.time()headers = {'X-Requested-With': 'XMLHttpRequest','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}pageSession = requests.Session()url = 'http://219.136.133.163:8000/Pages/Commonpage/AsyGetData.asmx/GetParkList'pageSession.get(url, headers=headers)data = {'cp': '1','ps': '10','kw': '','lon': 'undefined','lat': 'undefined','type': 'undefined'}if not os.path.exists('./Data'):os.mkdir('./Data')p_list = []for i in range(318):data['cp'] = str(i + 1)p = multiprocessing.Process(target=getAllJson, args=(pageSession, data, headers))p.start()p_list.append(p)if len(p_list) == 16:for p in p_list:p.join()p_list = []et = time.time()print(et - st)總結
以上是生活随笔為你收集整理的爬取广州所有停车场数据(Python)(并行加速版本)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【解决方案】Selenium打开网站却显
- 下一篇: 【2018年更新】Sublime tex