当前位置:网站首页>08 reptile project

08 reptile project

2022-07-24 03:16:00 Yinma Hanhai

 Insert picture description here
1. Collect the latest epidemic data of countries all over the world
 Insert picture description here
You have to put json Turn into python, use python The code gets the desired data , It's transforming into json Deposit in json In the document

 Insert picture description here

import requests
from bs4 import BeautifulSoup
import re
import json

# 1. Send a request , Get the epidemic Homepage 
response = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')
home_page = response.content.decode()  #  Get the homepage content of the response 

# 2. From the front page of the epidemic , Extract the latest epidemic data of various countries 
soup = BeautifulSoup(home_page, 'lxml')  #  structure BeautifulSoup object 
script = soup.find(id='getListByCountryTypeService2true')
# print(script) #  Describe its contents 

# ( Next I need this json Formatted data , You need to use regular extraction )


# 3. From the epidemic data , obtain json Format string 
json_str = re.findall(r'\[.+\]', script.string)[0]
# print(json_str) #  The standard json Format string 


# 4. hold json Format string to python type 
last_day_corona_virus = json.loads(json_str)
print(last_day_corona_virus)  #  So get a python Data of type ( External interview list , There's a dictionary in it , Each dictionary represents the epidemic information of a country )


# 5. With json Format preservation , Epidemic data of various countries on the latest day 
with open('data/last_day_corona_virus.json', 'w', encoding='UTF-8') as fp:
    json.dump(last_day_corona_virus, fp, ensure_ascii=False)

''' Be careful :  If there is an error here ‘gbk’ The decoder cannot be compiled , Can be in open() Specify the inside  encoding='UTF-8' that will do  '''

 Insert picture description here
2. Acquisition from 01 month 23 Epidemic data of countries all over the world since Japan
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[Yinma Hanhai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207222302403243.html