【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使用

相关推荐
你不是我我3 分钟前
【Java开发日记】说一说 SpringBoot 中 CommandLineRunner
java·开发语言·spring boot
心扬15 分钟前
python网络编程
开发语言·网络·python·tcp/ip
忧陌60618 分钟前
DAY 44 预训练模型
python
qq_4541757922 分钟前
c++学习-this指针
开发语言·c++·学习
点云SLAM34 分钟前
PyTorch 中contiguous函数使用详解和代码演示
人工智能·pytorch·python·3d深度学习·contiguous函数·张量内存布局优化·张量操作
尘浮7281 小时前
60天python训练计划----day45
开发语言·python
sss191s1 小时前
校招 java 面试基础题目及解析
java·开发语言·面试
哆啦A梦的口袋呀1 小时前
基于Python学习《Head First设计模式》第六章 命令模式
python·学习·设计模式
努力搬砖的咸鱼1 小时前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
Calvex1 小时前
PyCharm集成Conda环境
python·pycharm·conda