01数据采集
首先,我们打开开发者模式,然后输入蜜雪冰城进行搜索,我们发现,数据是以JSON形式存储在页面中的。
既然我们已经找到数据存储的真实网页和数据存储的形式了,那就可以开始发送请求,提取数据了,代码如下:
import requests params = ( ('newmap', '1'), ('reqflag', 'pcmap'), ('biz', '1'), ('from', ['webmap', 'webmap']), ('da_par', 'baidu'), ('pcevaname', 'pc4.1'), ('qt', 's'), ('da_src', 'searchBox.button'), ('wd', '\u871C\u96EA\u51B0\u57CE'), ('c', '1'), ('src', '0'), ('wd2', ''), ('pn', '0'), ('sug', '0'), ('l', '5'), ('b', '(6667763.599996051,6340854.629990179;19250675.599996053,7782646.629990179)'), ('biz_forward', '/{"scaler":2,"styles":"pl"/}'), ('sug_forward', ''), ('auth', '5BEZZCLEd4HKOxLFbExRB0JQAVTQfxEPuxLzRxLEEHEtBalTBnlcAZzvYgP1PcGCgYvjPuVtvYgPMGvgWv@uVtvYgPPxRYuVtvYgP@vYZcvWPCuVtvYgP@ZPcPPuVtvYgPhPPyheuVtvhgMuxVVty1uVtCGYuVt1GgvPUDZYOYIZuVt1cv3uVtGccZcuVtPWv3Guxt58Jv7uPYIUvhgMZSguxzBEHLNRTVtcEWe1GD8zv7u@ZPuVtc3CuVteuxtf0wd0vyMASyMFFFCufy9GUIsxC1w0kMMxNwxcc@AZ'), ('seckey', 'e7ccd76a71cca7384bc9d56993ddbed2e19bbff4744b85e39bb3d65be30e7613e76ae0b8689ae7f5bb14207898aef6950e69432a9314fa542a239fa64bfb5b4537c123bc41a4c69c3d897270e7703085fab6ef51a01edc960ca3bb277afd081ac8815ca7ad75f2032c5416f224ac384247adeed8f7371a70023518fb691f60aa2084c0839ba9d280dc940d93971effeae8b5de5e4658741578f88a9fb58aed60dcab2a678a32253e12afa1489c6135678e4b8f39428ad1f5e5760d39484d37d4934e5748a90eb62e5f802bd5836d29a5f3193491689fb3d3fa32277589ae66c8'), ('device_ratio', '2'), ('tn', 'B_NORMAL_MAP'), ('nn', '0'), ('u_loc', '12711424,4262547'), ('ie', 'utf-8'), ('t', '1628164454007'), ) response = requests.get('https://map.baidu.com/', headers=headers, params=params, cookies=cookies) r = response.json()['more_city']
采集到的数据如下:
数据已经采集到的了,我们现在将其存储到CSV文件中去,代码如下:
for i in r: province_name = i['province'] province_num = i['num'] with open('省份.csv','a',newline='') as f: writer = csv.writer(f) writer.writerow([province_name,province_num]) for j in i['city']: city_name = j['name'] city_num = j['num'] with open(f'{province_name}.csv','a',newline='') as f: writer_1 = csv.writer(f) writer_1.writerow([city_name,city_num]) with open('全部.csv','a',newline='') as f: writer_2 = csv.writer(f) writer_2.writerow([city_name,city_num])
02数据可视化
我们已经采集到的全国各省份的店铺数据,现在将其可视化展示。
从图中我们可以看出,河南省开的蜜雪冰城店铺最多,有1836家,想想这次河南洪灾,雪王自己受灾,还捐了2200W,真是想哭!
我们接着对河南省店铺地域分布进行可视化发现,郑州市的店铺最多,是587家,其次是周口市、洛阳市的店铺最多,分别是160家和112家。
郑州是河南这次受灾非常严重的地方,蜜雪冰城还捐出这么多钱,真是像极了老一辈平时省吃俭用,但是国家有需要的时候,毫不犹豫的把自己的积蓄捐出来!
所以这第一杯奶茶都给我去蜜雪冰城喝!!!
03小结
1. 本文详细介绍了,如何从某度地图上获取数据,后台回去[奶茶]即可获取全部源码。
2. 本文仅供学习参考,不做它用。