❶ 怎樣用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)