parent
4c61ea4c51
commit
c343017662
@ -0,0 +1,40 @@
|
||||
import requests
|
||||
import csv
|
||||
from lxml import etree
|
||||
from config import FILE_NAME, QUERY_URL, HEADERS
|
||||
|
||||
def fetch_raw_data():
|
||||
response = requests.get(QUERY_URL, headers=HEADERS)
|
||||
return response.text
|
||||
|
||||
|
||||
def parse_data(data):
|
||||
etree.HTMLParser(encoding = "utf-8")
|
||||
doc = etree.HTML(data)
|
||||
|
||||
# 国家
|
||||
country = doc.xpath('//div[@class="table_container"]//tbody/tr/td/span/text()')
|
||||
# 确诊人数
|
||||
person = doc.xpath('//div[@class="table_container"]//tbody/tr/td[2]/text()')
|
||||
person = [x.replace(",", "") for x in person]
|
||||
# 死亡人数
|
||||
death = doc.xpath('//div[@class="table_container"]//tbody/tr/td[3]/text()')
|
||||
death = [x.replace(",", "") for x in death]
|
||||
|
||||
message = list(zip(country, person, death))
|
||||
|
||||
return message
|
||||
|
||||
|
||||
def save_message(message, file_name):
|
||||
with open(file_name, "w", encoding = "utf-8") as file:
|
||||
w = csv.writer(file)
|
||||
w.writerows(message)
|
||||
|
||||
|
||||
def fetch_and_save():
|
||||
raw_data = fetch_raw_data()
|
||||
|
||||
message = parse_data(raw_data)
|
||||
|
||||
save_message(message, FILE_NAME)
|
Loading…
Reference in new issue