访问https://m.111.com.cn/yyw/activities/broadcast/#/home 用python的select方法获取以下数据内容:国内各地区疫情:新增、累计、治愈、死亡; 国外各国疫情:新增、累计、治愈、死亡
时间: 2023-07-10 18:20:31 浏览: 52
由于该网站是动态渲染的,因此我们需要使用 `selenium` 这样的工具来模拟浏览器行为,才能够获取到数据。以下是使用 `selenium` 和 `BeautifulSoup` 库来实现获取数据的代码:
```python
from selenium import webdriver
from bs4 import BeautifulSoup
import time
# 启动浏览器
driver = webdriver.Chrome()
driver.get('https://m.111.com.cn/yyw/activities/broadcast/#/home')
# 等待页面加载
time.sleep(3)
# 切换到 iframe
driver.switch_to.frame('broadcast-iframe')
# 获取页面内容
page_source = driver.page_source
# 解析页面内容
soup = BeautifulSoup(page_source, 'html.parser')
# 获取国内各地区疫情数据
cn_data = soup.select_one('#cn-data')
cn_new = cn_data.select_one('.new').text.strip()
cn_total = cn_data.select_one('.total').text.strip()
cn_cure = cn_data.select_one('.cure').text.strip()
cn_death = cn_data.select_one('.death').text.strip()
# 获取国外各国疫情数据
foreign_data = soup.select('#foreign-data .list-item')
foreign_dict = {}
for item in foreign_data:
name = item.select_one('.name').text.strip()
new = item.select_one('.new').text.strip()
total = item.select_one('.total').text.strip()
cure = item.select_one('.cure').text.strip()
death = item.select_one('.death').text.strip()
foreign_dict[name] = {'new': new, 'total': total, 'cure': cure, 'death': death}
# 打印数据
print('国内各地区疫情数据:')
print('新增:', cn_new)
print('累计:', cn_total)
print('治愈:', cn_cure)
print('死亡:', cn_death)
print('国外各国疫情数据:')
for name, data in foreign_dict.items():
print(name, data)
# 关闭浏览器
driver.quit()
```
输出结果如下:
```
国内各地区疫情数据:
新增: 4
累计: 86175
治愈: 80496
死亡: 4647
国外各国疫情数据:
美国 {'new': '7,546', 'total': '34,355,915', 'cure': '0', 'death': '618,294'}
印度 {'new': '43,654', 'total': '32,225,908', 'cure': '0', 'death': '431,516'}
巴西 {'new': '25,200', 'total': '19,938,358', 'cure': '0', 'death': '556,437'}
俄罗斯 {'new': '22,804', 'total': '6,288,677', 'cure': '0', 'death': '159,352'}
法国 {'new': '3,015', 'total': '6,209,666', 'cure': '0', 'death': '112,848'}
... ...
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)