❶ 怎样用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.mp(last_day_corona_virus, fp, ensure_ascii=False)
❷ 使用python编写函数,实现以下功能
这不就是遍历吗= =,怎么又是你
def fun(s):
ret = ''
for i,elem in enumerate(s):
ret += elem + ('*' if i % 2 == 0 and ord(elem) % 2 != 0 else '')
return ret使用
s = 'ABCDEFG12345'
print fun(s)