I am a programmer Xiaoye, bringing you original and wonderful technical content.
The epidemic situation is unbearable for both individuals and enterprises. I hope that the epidemic will pass as soon as possible, which is a small wish of ordinary people.
Relatives and friends around us are more or less affected by the epidemic. Let's find out Wuhan friends in the circle of friends through python to give them care.
The first step is to install wxpy first, and then the pyecharts module used in map drawing in this article
pip install wxpy # Support wechat related functions
pip install pyecharts # Support map drawing function
pip install pyecharts_snapshot
Step 2: download the required map data package as required. After pyecharts v0.3.2, pyecharts will no longer bring its own map js file. If you need to use the map chart, you can install the corresponding map file package. Here we choose the provincial map of China: echarts-china-provinces-pypkg to download and display the map of Hubei province
Global country map: ecrats countries pypkg: world map and 213 countries, including China
Provincial map of China: echarts China provinces pypkg: 23 provinces, 5 autonomous regions
City map of China: eckarts China cities pypkg: 370 Chinese cities
County level map of China: echarts China counts pypkg: 2882 counties and districts in China
China regional map: echarts China misc pypkg: 11 regional maps of China, such as South China and North China.
In addition, the UK 2016 constituency map: echarts United Kingdom pypkg: the UK constituency map can be used to draw data related to political economy
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-counties-pypkg
pip install echarts-china-misc-pypkg
When the environment is ready, the design strategy follows.
1. Get the friend information through Bot(cache_path=True).friends() function to find the Hubei friend we are looking for;
2. Add custom geojson data to eCharts to realize map display;
3. Custom blessing script, randomly sent to Hubei friends list;
Full code:
# encoding: utf-8 """ @author: Programmer Xiaoye @contact: 3203636266@qq.com @WeChat official account: programmers' little leaves @time: 2020/2/19 13:03 @file: case4.py @desc: At present, python Help you find out Wuhan friends in your circle of friends and give them care """ from wxpy import * from pyecharts import options as opts from pyecharts.charts import Geo from pyecharts.globals import ChartType from pyecharts.globals import ThemeType import webbrowser import random def is_Chinese(word): # Due to the presence of foreign friends, we need to filter out foreign English names and friends who do not fill in the area for ch in word: if '\u4e00' <= ch <= '\u9fff': return True return False bot = Bot(cache_path=True) # Get all your friends' information friends = bot.friends() # Get province information and filter out Hubei friends china_friends_sum = {} hubei_friends_sum = {} hubei_friends = [] for f in friends: province = f.province if is_Chinese(province): if province in china_friends_sum: china_friends_sum[province] += 1 if province == 'Hubei': city = f.city print(f.remark_name) print(f.city) hubei_friends.append(f) if city in hubei_friends_sum: hubei_friends_sum[city] += 1 else: hubei_friends_sum[city] = 1 else: china_friends_sum[province] = 1 # As the name of the city in wechat does not include "city", and some autonomous prefectures are also abbreviations, it is necessary to complete them, such as Enshi Tujia and Miao Autonomous Prefecture hubei_citys = ['Huanggang City', 'Huangshi City', 'Ezhou City', 'Wuhan City', 'Xianning', 'Xiaogan City', 'suizhou', 'Tianmen', 'Xiantao', 'jingzhou', 'Jingmen City', 'Qianjiang', 'Xiangyang', 'Yichang City', 'Shiyan City', 'Shennongjia Forestry District ', 'Enshi Tujia and Miao Autonomous Prefecture '] echart_data = [] max = 0 for m in hubei_friends_sum: for city in hubei_citys: if m in city: item = (city, hubei_friends_sum[m]) echart_data.append(item) if hubei_friends_sum[m] > max: max = hubei_friends_sum[m] print(echart_data) print(max) print(china_friends_sum) # Map distribution def geo_guangdong() -> Geo: c = ( Geo(init_opts=opts.InitOpts(theme=ThemeType.DARK)) .add_schema(maptype="Hubei") # It can also be another province, so the corresponding city name also needs to be modified .add("", echart_data, ChartType.EFFECT_SCATTER, is_selected=True, symbol=None, symbol_size=6, color="red") .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts(visualmap_opts=opts.VisualMapOpts(is_piecewise=True, max_=max, ), title_opts=opts.TitleOpts(title="Distribution of wechat friends in Hubei", pos_left="300") ) ) return c g = geo_guangdong() g.render('Distribution of wechat friends in Hubei.html') # Open the generated local web page with a browser # webbrowser.open("wechat friends distribution map of Hubei. html") # Send blessings to each other # Blessing quotations wishes = ["Go out and wear a mask! Wash your hands and ventilate frequently, and don't go out without gathering!", "Keep a good attitude, pay attention to protection and frequent disinfection, reduce going out and gathering, and protect yourself and your family.", "But do good deeds, don't ask about the future, and cross the difficulties together."] print(len(wishes)-1) for hf in hubei_friends: print(hf.remark_name) target = bot.search(hf.remark_name)[0] # Fill in the nickname of wechat friends here target.send(wishes[random.randint(0, len(wishes)-1)]) # Enter the Python command line to keep the program running embed()
If you are learning Python, you may as well pay attention to favorite!
The official account of the same name: programmer Xiao Ye, concerned about sending "data analysis", giving the "Python analysis of data" (Chinese bookmark) eBook (learning the best book of Python foundation Library).