zoukankan      html  css  js  c++  java
  • 爬取斗鱼房间的信息

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    说明:仅学习参考,如有侵权,将立即删除此内容

    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    version_1
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    目标:爬去斗鱼的房间信息
    domain = "https://www.douyu.com/g_yz"

    技术:selenium
    难点:
    (1)在通过find_element_by_xpath选择元素的时候,一定要注意标签内属性的值,前后是否有空格,
    这是最容易出错的地方,如" hahah"或"hahah "等情况,结合网页源码一起看。
    (2)需要强制等待,以让网页显示完整的数据,方便提取数据。

    说明:
    (1)通过find_element_by_xpath来找元素很慢,理想的使用场景是:通过selenium抓取到网页源代码,
    然后通过xpath或bs4或正则,来提取关键信息,最后通过find_elements_by_xpath来提取下一页地址,
    然后循环上一步。
    也就是说只通过selenium来实现网页源代码的抓取,模拟人的点击操作或链式操作,
    然后通过xpath或bs4或正则,来提取关键信息。
    (2)需要使用强制等待,即time.sleep(),时间的长度,可以由函数生成,不过要保证最低等待时间。
    (3)本例使用的是"https://www.douyu.com/g_yz",只是抓取了部分内容,但是斗鱼的网页结构性很强,所以能够通用。
    将self.start_url换成任何分类下的首网址即可复用,如“https://www.douyu.com/directory/all”抓取所有内容,
    也行得通。

    源码:

    from selenium import webdriver
    import time
    import json
    
    class DySpider():
        def __init__(self):
            self.start_url = 'https://www.douyu.com/g_yz'
            self.driver = webdriver.Chrome()
        
        def parse_source(self):
            # 强制等待10s
            time.sleep(10)
            # 分组
            li_list = self.driver.find_elements_by_xpath("//ul[@class='layout-Cover-list']/li")
            print("当前页内容数量",len(li_list))
            li_item = list()
            for li in li_list:
                item = dict()
                item["room_title"] = li.find_element_by_xpath(".//h3[@class='DyListCover-intro']").text
                item["room_image"] = li.find_element_by_xpath(".//img[@class='DyImg-content is-normal ']").get_attribute("src")
                item["room_href"] = li.find_element_by_xpath(".//a[@class='DyListCover-wrap']").get_attribute("href")
                item["room_anchor"] = li.find_element_by_xpath(".//h2[@class='DyListCover-user']").text
                item["room_tag"] = li.find_element_by_xpath(".//span[@class='DyListCover-zone']").text
                item["room_hot"] = li.find_element_by_xpath(".//span[@class='DyListCover-hot']").text
                print(item)
                li_item.append(item)
            next_page_url = self.driver.find_elements_by_xpath("//div[@class='ListFooter']//li[@class=' dy-Pagination-next']")
            print("next_page_url:",next_page_url)
            next_page_url = next_page_url[0] if len(next_page_url)>0 else None
            return li_item,next_page_url
    
        def savefile_to_json(self,filename,content):
            with open(filename,'w',encoding='utf8') as f:
                f.write(json.dumps(content,ensure_ascii=False,indent=2))
                print("保存"+filename+"文件成功")
    
        def run(self):
            # 请求第一个url
            self.driver.get(self.start_url)
    
            # 处理get到的内容,获取下页地址
            li_item,next_page_url = self.parse_source()
    
            # 保存内容到列表
            room_content = list()
            room_content.append(li_item)
    
            # 循环处理下一页
            while next_page_url is not None:
                next_page_url.click()
                # 处理get到的内容,获取下页地址
                li_item, next_page_url = self.parse_source()
                # 保存内容到列表
                room_content.append(li_item)
    
            self.savefile_to_json("斗鱼_颜值.json",room_content)
    
            self.driver.close()
    
    if __name__=='__main__':
        obj = DySpider()
        obj.run()
  • 相关阅读:
    基础算法:两数之和
    adb常用命令食用方法
    C语言面试题
    C语言经典笔试题目
    嵌入式01 51单片机实验02 sfr和sbit bit和int char
    C语言 01 volatile关键字
    C++ 01 基础知识点
    嵌入式02 STM32 实验11 NVIC和中断总结
    嵌入式02 STM32 实验10 定时器中断
    嵌入式02 STM32 实验09 独立/窗口看门狗
  • 原文地址:https://www.cnblogs.com/nuochengze/p/12930212.html
Copyright © 2011-2022 走看看