爬虫之selenium爬取斗鱼网站
2023-09-11 14:15:15 时间
爬虫之selenium爬取斗鱼网站
示例代码:
from selenium import webdriver
import time
class Douyu(object):
def __init__(self):
self.url = 'https://www.douyu.com/directory/all'
self.driver = webdriver.Chrome()
def parse_data(self):
time.sleep(3)
room_list = self.driver.find_elements_by_xpath('//*[@id="listAll"]/section[2]/div[2]/ul/li/div/a')
print(len(room_list))
data_list = []
for room in room_list:
temp = {}
temp['title'] = room.find_element_by_xpath('./div[2]/div[1]/h3').text
temp['type'] = room.find_element_by_xpath('./div[2]/div[1]/span').text
data_list.append(temp)
return data_list
def save_data(self, data_list):
for data in data_list:
print(data)
def run(self):
# url
# driver
# get
self.driver.get(self.url)
# 用于判断首页是否有广告
try:
time.sleep(6)
self.driver.find_element_by_xpath('/html/body/div[2]/span[1]').click() 【此处需要等待几秒把弹窗关闭】
except Exception as e:
print(e)
while True:
# parse
data_list = self.parse_data()
# save
self.save_data(data_list)
# next
try:
# el_next = self.driver.find_element_by_xpath('//*[contains(text(),"下一页")]')
el_next = self.driver.find_element_by_xpath('//*[@id="listAll"]/section[2]/div[2]/div/ul/li[9]/span')
self.driver.execute_script('scrollTo(110,100000)')
el_next.click()
except:
break
if __name__ == '__main__':
douyu = Douyu()
douyu.run()
运行效果:
思路用图:
相关文章
- 关于网站爬虫的爬取
- Python 爬虫与HTTP协议简介
- 【nodeJS爬虫】前端爬虫系列 -- 小爬「博客园」
- 爬虫之xpath语法-基础节点选择语法
- 爬虫之selenium对cookie的处理
- 爬虫性能分析及优化
- 30 爬虫 - 爬取内涵段子网站案例
- 86 爬虫 - scrapy-redis源码分析(pipelines)
- 62 爬虫 - Tesseract从网站图片中抓取文字
- 16 爬虫 - Requests 私密代理验证 和 Web客户端验证
- 不编程也能爬虫?手把手教你如何从互联网采集海量数据
- 220704 wetchat爬虫目标
- Python网络爬虫 - 一个简单的爬虫例子
- Python--爬虫,了解防盗链-->解决防盗链,解决反反反爬
- 网络爬虫数据存储MySQL和Mongodb方法汇总
- python 爬虫之序列化html问题(类似u003)(去掉html中的标签比如<em>)
- 爬虫_Crawler4j的使用
- Python爬虫之Urllib(内置库)
- Python爬虫练习二-定时抓取网站的更新内容
- Scrapy框架: 通用爬虫之SitemapSpider
- 爬虫流程复习
- Python乐园提供Python学习的基地,免费分享与提供基础学习、高级开发、有趣的爬虫、人工智能、系统网站开发、前沿的最新技术、项目架构、以及配套的资料、视频、源码、笔记等等
- 爬虫学习:Urllib的使用