python爬虫利用selenium实现自动翻页爬取某鱼数据的思路详解
作者:孤城暮雨 时间:2021-05-20 08:43:26
基本思路:
首先用开发者工具找到需要提取数据的标签列
利用xpath定位需要提取数据的列表
然后再逐个提取相应的数据:
保存数据到csv:
利用开发者工具找到下一页按钮所在标签:
利用xpath提取此标签对象并返回:
调用点击事件,并循环上述过程:
最终效果图:
代码:
from selenium import webdriver
import time
import re
class Douyu(object):
def __init__(self):
# 开始时的url
self.start_url = "https://www.douyu.com/directory/all"
# 实例化一个Chrome对象
self.driver = webdriver.Chrome()
# 用来写csv文件的标题
self.start_csv = True
def __del__(self):
self.driver.quit()
def get_content(self):
# 先让程序两秒,保证页面所有内容都可以加载出来
time.sleep(2)
item = {}
# 获取进入下一页的标签
next_page = self.driver.find_element_by_xpath("//span[text()='下一页']/..")
# 获取用于判断是否是最后一页的属性
is_next_url = next_page.get_attribute("aria-disabled")
# 获取存储信息的所有li标签的列表
li_list = self.driver.find_elements_by_xpath("//ul[@class='layout-Cover-list']//li")
# 提取需要的数据
for li in li_list:
item["user-id"] = li.find_element_by_xpath(".//div[@class='DyListCover-userName']").text
item["img"] = li.find_element_by_xpath(".//div[@class='DyListCover-imgWrap']//img").get_attribute("src")
item['class-name'] = li.find_element_by_xpath(".//span[@class='DyListCover-zone']").text
item["click-hot"] = li.find_element_by_xpath(".//span[@class='DyListCover-hot']").text
item["click-hot"] = re.sub(r'\n','',item['click-hot'])
# 保存数据
self.save_csv(item)
# 返回是否有下一页和下一页的点击事件的标签,
return next_page,is_next_url
def save_csv(self,item):
# 将提取存放到csv文件中的内容连接为csv格式文件
str = ','.join([i for i in item.values()])
with open('./douyu.csv','a',encoding='utf-8') as f:
if self.start_csv:
f.write("用户id,image,所属类,点击热度\n")
self.start_csv = False
# 将字符串写入csv文件
f.write(str)
f.write('\n')
print("save success")
def run(self):
# 启动chrome并定位到相应页面
self.driver.get(self.start_url)
while True:
# 开始提取数据,并获取下一页的元素
next_page,is_next = self.get_content()
if is_next!='false':
break
# 点击下一页
next_page.click()
if __name__=='__main__':
douyu_spider = Douyu()
douyu_spider.run()
来源:https://www.cnblogs.com/aa1273935919/archive/2020/12/21/14168439.html
标签:python,爬虫,selenium
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python函数基础
2022-09-11 11:36:43
![](https://img.aspxhome.com/file/2023/7/75197_0s.png)
改善登陆界面的用户体验: 自动聚焦表单
2009-12-09 16:13:00
![](https://img.aspxhome.com/file/UploadPic/200912/9/js_auto_focus_1-62s.png)
python命令行参数argparse模块基本用法详解
2023-07-31 03:14:21
![](https://img.aspxhome.com/file/2023/2/59282_0s.png)
Python数据挖掘中常用的五种AutoEDA 工具总结
2022-05-20 02:49:51
![](https://img.aspxhome.com/file/2023/3/102853_0s.png)
随Linux开机自动启动mysql
2009-12-29 10:14:00
降低python版本的操作方法
2022-04-18 18:28:02
![](https://img.aspxhome.com/file/2023/4/81484_0s.png)
利用python汇总统计多张Excel
2023-12-31 14:22:11
![](https://img.aspxhome.com/file/2023/0/77690_0s.jpg)
python模型集成知识点总结
2023-08-03 00:16:16
asp的日期转换星座函数
2010-06-09 21:05:00
ASP Session对象的集合以及属性方法事件
2009-02-02 09:26:00
python海龟绘图之画国旗实例代码
2022-03-06 14:57:23
![](https://img.aspxhome.com/file/2023/5/86575_0s.jpg)
python实现图片转换成素描和漫画格式
2023-09-20 14:44:25
![](https://img.aspxhome.com/file/2023/1/80711_0s.jpg)
服务器端的代码是如何被解释成客户端的?
2009-11-01 15:15:00
再说淘宝的评价和信用机制
2008-07-10 12:43:00
Python3中多线程编程的队列运作示例
2022-06-15 23:27:49
keras 指定程序在某块卡上训练实例
2023-07-15 09:15:42
![](https://img.aspxhome.com/file/2023/1/99041_0s.jpg)
速记Python布尔值
2022-04-23 10:02:17
Python字符串的15个基本操作(小结)
2023-08-11 00:11:13
Python数据可视化之简单折线图的绘制
2021-05-25 11:59:30
![](https://img.aspxhome.com/file/2023/1/99181_0s.jpg)
asp Access数据备份,还原,压缩类代码
2011-03-07 11:16:00