【Python爬虫】案例_斗鱼

声明:案例只用于学习,不得恶意使用

要求:获取直播间标题、类型、主播、热度,并实现翻页

定位随着网站更新可能不会实现,请自行更改

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

chrome_options = Options()
chrome_options.page_load_strategy = 'eager'
service = Service('chromedriver.exe路径')

class Douyu(object):

    def __init__(self):
        self.url = 'https://www.douyu.com/directory/all'
        self.driver = webdriver.Chrome(service=service, options=chrome_options)
        self.driver.implicitly_wait(5)

    def parse_data(self):
        time.sleep(3)
        data_list= []
        # 遍历房间列表,从每一个房间节点中获取数据
        for i in range(1,121):
            temp = {}
            temp['title'] = self.driver.find_element(By.XPATH, f'//li[{i}]/div/a/div[2]/div[1]/h3').text
            temp['type'] = self.driver.find_element(By.XPATH, f'//li[{i}]/div/a/div[2]/div[1]/span').text
            temp['owner'] = self.driver.find_element(By.XPATH, f'//li[{i}]/div/a/div[2]/div[2]/h2').text
            temp['num'] = self.driver.find_element(By.XPATH, f'//li[{i}]/div/a/div[2]/div[2]/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):
        self.driver.get(self.url)
        while True:
            data_list = self.parse_data()
            self.save_data(data_list)

            try:
                el_next = self.driver.find_element(By.XPATH, '//*[@title="下一页"][@aria-disabled="false"]')
                self.driver.execute_script('scrollTo(0,1000000)')
                el_next.click()
            except:
                break

if __name__ == '__main__':
    douyu = Douyu()
    douyu.run()

【Python爬虫】Selenium使用

相关推荐
合作小小程序员小小店24 分钟前
SDN安全开发环境中常见的框架,工具,第三方库,mininet常见指令介绍
python·安全·生成对抗网络·网络安全·网络攻击模型
后台开发者Ethan27 分钟前
Python需要了解的一些知识
开发语言·人工智能·python
weixin_4433533132 分钟前
小红书帖子评论的nodejs爬虫脚本
前端·爬虫
北京_宏哥36 分钟前
Python零基础从入门到精通详细教程11 - python数据类型之数字(Number)-浮点型(float)详解
前端·python·面试
盼小辉丶1 小时前
PyTorch生成式人工智能——使用MusicGen生成音乐
pytorch·python·深度学习·生成模型
常利兵1 小时前
Kotlin作用域函数全解:run/with/apply/let/also与this/it的魔法对决
android·开发语言·kotlin
幼稚园的山代王1 小时前
Kotlin-基础语法练习一
android·开发语言·kotlin
重生成为编程大王2 小时前
Java ConcurrentHashMap 深度解析
java·开发语言
tanyongxi662 小时前
C++ 特殊类设计与单例模式解析
java·开发语言·数据结构·c++·算法·单例模式
遗憾皆是温柔2 小时前
24. 什么是不可变对象,好处是什么
java·开发语言·面试·学习方法