下载当天各省疫情数据

下载疫情数据表格

  1. 使用driver建立链接,chromedriver.exe放置在项目目录中
import pandas as pd
from selenium import webdriver
from bs4 import BeautifulSoup

url = 'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_pc_3'
driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.get(url)
  1. 使用soup获取表格
text = driver.page_source
soup = BeautifulSoup(text, 'lxml')
 # 获取表格,写class的属性具体到哪一个
table = soup.find('table', {'class': 'VirusTable_1-1-348_38pQEh'})
results = table.find_all('tr')
  1. 格式化数据并保存到excel中
    rows = []
    for result in results:
        data = result.find_all("td")
        if len(data) == 0:
            continue
        area = data[0].find_all('span')[1].getText()
        confirm = data[1].getText()
        asymptomatic = data[2].getText()
        confirmSum = data[3].getText()
        riskArea = data[4].getText()
        rows.append([area, confirm, asymptomatic, confirmSum, riskArea])
    df = pd.DataFrame(rows)
    # 设置列名
    df.columns = ["省市地区", "新增确诊", "新增无症状", "累计确诊", "风险地区"]
    print(df)
    # 输出到excel
    df.to_excel('outputSum.xlsx', index=None)

这种方法需要去访问url,所以速度较慢,适用于数据不多的场景

你可能感兴趣的:(python,python,开发语言)