selenium爬虫 | 爬取疫情实时动态
import csv
import selenium.webdriver
from selenium.webdriver.chrome.options import Options
class spider():
def get_msg(self,url):
global timeNum, provinceDic
# 无窗口弹出操作
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver=selenium.webdriver.Chrome(options=options)
driver.get(url)
timeNum=driver.find_element_by_xpath('//*[@id="charts"]/div[2]/span[1]').text#实时
icbar_confirm=driver.find_element_by_xpath('//*[@id="charts"]/div[3]/div[1]/div[1]').text#全国确诊数
icbar_suspect=driver.find_element_by_xpath('//*[@id="charts"]/div[3]/div[2]/div[1]').text#疑似病例数
icbar_cure=driver.find_element_by_xpath('//*[@id="charts"]/div[3]/div[3]/div[1]').text#治愈人数
icbar_dead=driver.find_element_by_xpath('//*[@id="charts"]/div[3]/div[4]/div[1]').text#死亡人数
print("{}\n全国确诊:{}\n疑似病例:{}\n治愈人数:{}\n死亡人数:{}\n".format(timeNum, icbar_confirm, icbar_cure, icbar_dead,icbar_suspect))
place_current=driver.find_elements_by_css_selector('div[class="place current"]')#湖北省的数据
place = driver.find_elements_by_css_selector('div[class="place"]')#其他省的数据
place_= driver.find_elements_by_css_selector('div[class="place "]')#其他省的数据
place_no_sharp = driver.find_elements_by_css_selector("div[class='place no-sharp ']")#自治区的数据
tplt = "{0:{4}<10}\t{1:{4}<15}\t{2:{4}<15}\t{3:{4}<15}"
print(tplt.format("地区","确诊人数","治愈人数","死亡人数",chr(12288)) + "\n")
# 建立一个字典,键为省名,值为省的具体数据
provinceDic=dict()
provinceDic["全国"]=["全国",icbar_confirm, icbar_cure, icbar_dead, icbar_suspect]
places = place_current + place + place_ + place_no_sharp # 所有的行省的数据列表合集
for place in places:
# print(place.text)
name=place.find_element_by_css_selector("span[class='infoName']").text
confirm=place.find_element_by_css_selector("span[class='confirm'] span").text
try:
heal=place.find_element_by_css_selector("span[class='heal '] span").text
except:
heal = place.find_element_by_css_selector("span[class='heal hide'] span").text
try:
dead=place.find_element_by_css_selector("span[class='dead '] span").text
except:
dead=place.find_element_by_css_selector("span[class='dead hide'] span").text
print(tplt.format(name,confirm,heal,dead,chr(12288)))
provinceDic[name]=[name,confirm,heal,dead]
def save_data_as_csv(self,filename,dataList):
# filename="_".join(time.split(":"))
filename=filename.replace(":"," ")#调整时间
with open(filename+".csv","w",newline="") as f:
writer=csv.writer(f)
writer.writerow(["地区","确诊人数","治愈人数","死亡人数","疑似病例"])
for i in dataList:
writer.writerow(i)
f.close()
def main(self):
url = "https://news.qq.com/zt2020/page/feiyan.htm"
self.get_msg(url)
self.save_data_as_csv(timeNum,provinceDic.values())
billie=spider()
billie.main()
selenium爬虫 | 爬取疫情实时动态的更多相关文章
- selenium爬虫 | 爬取疫情实时动态(二)
'''@author:Billie更新说明:1-28 17:00 项目开始着手,spider方法抓取到第一条疫情数据,save_data_csv方法将疫情数据保存至csv文件1-29 13:12 目标 ...
- 使用selenium再次爬取疫情数据(链接数据库)
爬取网页地址: 丁香医生 数据库连接代码: def db_connect(): try: db=pymysql.connect('localhost','root','zzm666','payiqin ...
- [python爬虫] Selenium定向爬取PubMed生物医学摘要信息
本文主要是自己的在线代码笔记.在生物医学本体Ontology构建过程中,我使用Selenium定向爬取生物医学PubMed数据库的内容. PubMed是一个免费的搜寻引擎,提供生物医学方 ...
- Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页
Python3.x:Selenium+PhantomJS爬取带Ajax.Js的网页 前言 现在很多网站的都大量使用JavaScript,或者使用了Ajax技术.这样在网页加载完成后,url虽然不改变但 ...
- 使用Python爬虫爬取网络美女图片
代码地址如下:http://www.demodashi.com/demo/13500.html 准备工作 安装python3.6 略 安装requests库(用于请求静态页面) pip install ...
- python3爬虫爬取网页思路及常见问题(原创)
学习爬虫有一段时间了,对遇到的一些问题进行一下总结. 爬虫流程可大致分为:请求网页(request),获取响应(response),解析(parse),保存(save). 下面分别说下这几个过程中可以 ...
- selenium登录爬取知乎出现:请求异常请升级客户端后重试的问题(用Python中的selenium接管chrome)
一.问题使用selenium自动化测试爬取知乎的时候出现了:错误代码10001:请求异常请升级客户端后重新尝试,这个错误的产生是由于知乎可以检测selenium自动化测试的脚本,因此可以阻止selen ...
- 使用selenium 多线程爬取爱奇艺电影信息
使用selenium 多线程爬取爱奇艺电影信息 转载请注明出处. 爬取目标:每个电影的评分.名称.时长.主演.和类型 爬取思路: 源文件:(有注释) from selenium import webd ...
- Python爬虫 - 爬取百度html代码前200行
Python爬虫 - 爬取百度html代码前200行 - 改进版, 增加了对字符串的.strip()处理 源代码如下: # 改进版, 增加了 .strip()方法的使用 # coding=utf-8 ...
随机推荐
- Java中字段赋值顺序的问题
static字段 public class Client { public static int i = 2; static { i = 100; } public static void main( ...
- vue在html使用
1.Vue: 定义:渐进式JavaScript框架 渐进式: 定义:声明渲染 组件系统 客户端路由 集中式状态管理 项目构建 2.MVVM 定义 M Model(服务器上的业务逻辑操作) V View ...
- js下 Day07、DOM案例
一.折叠菜单 效果图: 功能思路分析: 功能一:数据渲染 \1. 模拟数据,一级数据为数组套对象的形式,二级数据为数组: \2. 先渲染一级数据,然后再嵌套渲染二级数据(map().join('')) ...
- 移动端 canvas基础1
一.canvas画布 Canvas是HTML5中新出的一个元素,开发者可以通过JS脚本动态绘制图像. #1. 创建canvas画布 在页面中创建canvas标签,并设置其id和宽高 (不要通过css设 ...
- 工具-效率工具-XMIND8破解(99.1.3)
@ 目录 1.下载 2.修改hosts文件 3.修改配置文件 4.填入序列号 5.破解完成 关于作者 1.下载 1.点击进入官方网站下载 2.下载破解包 网址:点击进入网盘地址 密码:domd 2.修 ...
- 爱普生 L4160 Serveies 网络打印机配置(问题解决)
一.爱普生网络打印机固定IP地址 用网络打印机过程中,偶尔会出现打印机脱机的状况,大多数原因是打印机的IP地址在路由器重启过后重新分配了IP地址导致的.此时,为了减少不必要的麻烦就需要固定打印机的IP ...
- Kafka Eagle 管理平台
Kafka-Eagle简介 源代码地址:https://github.com/smartloli/kafka-eagle Kafka Eagle是什么 Kafka Eagle是一款用于监控和管理Apa ...
- 微服务 - 服务消费(六)Ribbon
Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具.它是一个基于HTTP和TCP的客户端负载均衡器.它可以通过在客户端中配置ribbonServer ...
- 为什么游戏公司的server不愿意微服务化?
背景介绍 笔者最近去面试了家游戏公司(有上市).我问他,公司有没有做微服务架构的打算及考量?他很惊讶的,我没听说过微服务耶,你可以解释一下吗? 我大概说了,方便测试,方便维护,方便升级,服务之间松耦合 ...
- [leetcode349]Intersection of Two Arrays
设计的很烂的一道题 List<Integer> res = new ArrayList<>(); // int l1 = nums1.length; // int l2 = n ...