怎样用python爬取疫情数据

如题所述

import requests
from bs4 import BeautifulSoup
import re
import json

# 1.发送请求,获取疫情首页(数据来源于丁香园)
response = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')
home_page = response.content.decode()

# 2.从疫情首页提取最近一日数据
soup = BeautifulSoup(home_page, 'lxml')
script = soup.find(id='getAreaStat')
text = script.string

# 3.提取数据获取json格式数据
json_str = re.findall(r'\[.+\]', text)[0]

# 4.把json格式转换为python类型
last_day_corona_virus = json.loads(json_str)

# 5.以json格式保存最近一日数据
with open('data/last_day_coronavirus.json', 'w') as fp:
json.dump(last_day_corona_virus, fp, ensure_ascii=False)
温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-08-02
八爪鱼采集器是一款功能全面、操作简单、适用范围广泛的互联网数据采集器。它可以帮助用户快速抓取互联网上的各种数据,包括文字、图片、视频等多种格式。八爪鱼采集器使用简单且完全可视化操作,无需编写代码,内置海量模板,支持任意网络数据抓取。对于您的问题,如果您想使用Python爬取疫情数据,可以使用Python的网络爬虫库,如BeautifulSoup、Scrapy等。您可以编写Python脚本,通过发送HTTP请求获取疫情数据的网页内容,然后使用相应的解析库提取所需的数据。了解更多八爪鱼采集器的功能与合作案例,请前往官网了解更多详情。
相似回答