python selenium爬取斗鱼所有直播房间信息过程详解

yipeiwu_com6年前Python爬虫

还是分析一下大体的流程:

首先还是Chrome浏览器抓包分析元素,这是网址:https://www.douyu.com/directory/all

发现所有房间的信息都是保存在一个无序列表中的li中,所以我们可以先获取一个装有li的element对象的列表,然后在对每个element逐一操作

分析斗鱼的翻页,有一个下一页按钮,是个li,class="dy-Pagination-item-custom" ,但是当烦到最后一页的时候,class="dy-Pagination-disabled dy-Pagination-next",所以我们要想利用selenium模拟点击这个按钮,我们应该利用

get_elements_by_xpath()这个函数,这样到最后一页就获取不到了,就可以终止程序了。而用elements的原因是当到最后一页的时候获取不到的话,element会报错

然后还是通用的套路:发送请求获取响应、提取数据和下一页的元素、保存数据、点击下一页的元素循环......

遇到的两个坑:

需要用time.sleep()函数强制等待页面加载完再获取元素,否则报错,睡几秒就看你的网速了

xpath定位的时候,网页上有的类如这样:class=" abc"或者class="abc " ,前面或后面有空格的,xpath处理的时候也必须有空格,否则获取不到

代码实现:

import time
from selenium import webdriver
class DouyuSpider(object):
  def __init__(self):
    self.start_rul = 'https://www.douyu.com/directory/all'
    self.driver = webdriver.Chrome()

  def get_content_list(self):
    time.sleep(10) # 强制等待10秒,否则可能报错
    li_list = self.driver.find_elements_by_xpath('//ul[@class="layout-Cover-list"]/li')
    content_list = []
    for li in li_list:
      item = {}
      item['room_img'] = li.find_element_by_xpath('.//img[@class="DyImg-content is-normal "]').get_attribute('src')
      item['room_title'] = li.find_element_by_xpath('.//h3[@class="DyListCover-intro"]').text
      item['root_category'] = li.find_element_by_xpath('.//span[@class="DyListCover-zone"]').text
      item['author_name'] = li.find_element_by_class_name('DyListCover-user').text
      item['watch_num'] = li.find_element_by_class_name('DyListCover-hot').text
      content_list.append(item)
      print(item) # 打印每次获取到的直播房间的信息
    # 获取下一页的元素,为了防止没有报错,这里用elements,翻到最后一页一定就没有了,返回一个列表
    next_url = self.driver.find_elements_by_xpath('//li[@class=" dy-Pagination-next"]')
    next_url = next_url[0] if len(next_url) > 0 else None
    return content_list, next_url
  def save_content_list(self, content_list):
    pass # 保存数据这里就不再做演示

  def run(self): # 实现主要逻辑
    # 1.start_url
    # 2.发送请求,获取响应
    self.driver.maximize_window()
    self.driver.get(self.start_rul)
    # 3.提取数据,提取下一页的元素
    content_list, next_url = self.get_content_list()
    # 4.保存数据
    self.save_content_list(content_list)
    # 4.点击下一页元素,循环
    while next_url is not None:
      next_url.click()
      content_list, next_url = self.get_content_list()
      self.save_content_list(content_list)
if __name__ == '__main__':
  douban = DouyuSpider()
  douban.run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Scrapy框架爬取西刺代理网免费高匿代理的实现代码

Scrapy框架爬取西刺代理网免费高匿代理的实现代码

分析 需求: 爬取西刺代理网免费高匿代理,并保存到MySQL数据库中。 这里只爬取前10页中的数据。 思路: 分析网页结构,确定数据提取规则 创建Scrapy项目 编写...

python抓取京东小米8手机配置信息

python抓取京东小米8手机配置信息

本文代码是使用python抓取京东小米8手机的配置信息 首先找到小米8商品的链接:https://item.jd.com/7437788.html 然后找到其配置信息的标签,我们找到其配...

讲解Python的Scrapy爬虫框架使用代理进行采集的方法

1.在Scrapy工程下新建“middlewares.py” # Importing base64 library because we'll need it ONLY in cas...

Python爬虫爬取一个网页上的图片地址实例代码

Python爬虫爬取一个网页上的图片地址实例代码

本文实例主要是实现爬取一个网页上的图片地址,具体如下。 读取一个网页的源代码: import urllib.request def getHtml(url): html=urll...

零基础写python爬虫之HTTP异常处理

先来说一说HTTP的异常处理问题。当urlopen不能够处理一个response时,产生urlError。不过通常的Python APIs异常如ValueError,TypeError等也...