爬取美国肺炎疫情 url = https://coronavirus.1point3acres.com/zh
代码如下:(Jupyter Notebook)
import re,json import requests import pandas as pd url = 'https://coronavirus.1point3acres.com/_next/static/chunks/858271f81a54c4a12bd9f8d541482a95960c3031.f6d0ebde501d7c08cace.js' headers = {'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36", "referer":"coronavirus.1point3acres.com"} res = requests.get(url,headers=headers).text #data = re.findall(r"provinceShortName(.+?)curedCount",res) data = re.findall(r"provinceShortName(.+?deadCount.....)",res) df = pd.DataFrame(columns= ['Location','Confirmed','Cure','Dead']) for i in range(len(data)): a = '{"s' + data[i] a = a[0:-2] a = a + "}" a = eval(a) Loaction = a['provinceName'] Confirmed = a['confirmedCount'] Cure = a['curedCount'] Dead = a['deadCount'] df.loc[i] = [Loaction,Confirmed,Cure,Dead] # df df.to_excel('American_4_5_2.xlsx') import matplotlib.pyplot as plt #设置图片大小 plt.figure(figsize=(30,10),dpi =500) #画图 plt.plot(df.Location,df.Confirmed,'b.-') #设置x轴的刻度大小 plt.xticks(df.Location,rotation=90) #添加标题等信息 plt.xlabel("Location") plt.ylabel("Confirmed Numbers") plt.title("USA") plt.show()
此次的实战感谢博主 参考链接:https://blog.csdn.net/weixin_41846769/article/details/105193754