爬虫:国内疫情查询

国内疫情查询

# -*- coding: utf-8 -*-
"""
@File    :  Epidemic_data.py
@Time    :  2021/1/13 20:29
@Author  :  Mr.Fu
@Contact :  1766935706@qq.com
@Software:  PyCharm
"""
import bs4
from selenium import webdriver

print('请输入要查询的疫情地区(请以省为单位,例:河北):')
area_str = input()
print('查询中......')
print('查询结果:')
# 国内疫情实时数据
URL = 'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner'
# browser = webdriver.Chrome()
# 无头浏览器
options = webdriver.ChromeOptions()
options.add_argument('--headless')
# 添加一个参数设置使用无头浏览器(没有浏览器界面)
browser = webdriver.Chrome(options=options)
browser.get(URL)
more_button = browser.find_element_by_xpath('//*[@id="nationTable"]/div')
more_button.click()
soup = bs4.BeautifulSoup(browser.page_source, 'html.parser')
print('国内疫情数据')
update_time = browser.find_element_by_xpath('//*[@id="ptab-0"]/div[1]/div[1]/div[2]/span')
print(update_time.text)
sum = 35
dict = {}
list = []
for i in range(1, sum):
    area = browser.find_element_by_xpath(f'//*[@id="nationTable"]/table/tbody/tr[{i}]/td[1]/div/span[2]')
    # print(f'疫情地区:{area.text}')
    dict['疫情地区:'] = area.text
    add = browser.find_element_by_xpath(f'//*[@id="nationTable"]/table/tbody/tr[{i}]/td[2]')
    # print(f'新增:{add.text}')
    dict['新增:'] = add.text
    now = browser.find_element_by_xpath(f'//*[@id="nationTable"]/table/tbody/tr[{i}]/td[3]')
    # print(f'现有:{now.text}')
    dict['现有:'] = now.text
    add_up = browser.find_element_by_xpath(f'//*[@id="nationTable"]/table/tbody/tr[{i}]/td[4]')
    # print(f'累计:{add_up.text}')
    dict['累计:'] = add_up.text
    healing = browser.find_element_by_xpath(f'//*[@id="nationTable"]/table/tbody/tr[{i}]/td[5]')
    # print(f'治愈:{healing.text}')
    dict['治愈:'] = healing.text
    death = browser.find_element_by_xpath(f'//*[@id="nationTable"]/table/tbody/tr[{i}]/td[6]')
    # print(f'死亡:{death.text}')
    dict['死亡:'] = death.text
    # print('***我是分哥线***')
    list.append(dict.copy())

for j in range(sum - 1):
    if list[j]['疫情地区:'] == area_str:
        print(str(list[j])[1:-1])
上一篇:Python 光学文字识别


下一篇:python学习日记:splinter库中的browser.windows操作