python3_scrapy爬取斗鱼手机端数据包(颜值模块主播照片)

本文主要是介绍python3_scrapy爬取斗鱼手机端数据包(颜值模块主播照片),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.手机端数据包的抓取(响应教程可自行百度)


2.具体程序编写

items.py

# -*- coding: utf-8 -*-# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
import scrapyclass DouyuItem(scrapy.Item):# define the fields for your item here like:# 1.主播所在版块game_name = scrapy.Field()# 2.主播房间号room_id = scrapy.Field()# 3.主播链接vertical_src = scrapy.Field()# 4.主播昵称nickname = scrapy.Field()# 存储照片名称信息5.message = scrapy.Field()

douyu.py

# -*- coding: utf-8 -*-
import scrapy
import json
from Douyu.items import DouyuItemclass DouyuSpider(scrapy.Spider):name = 'douyu'allowed_domains = ['douyucdn.cn']# 0.抓取斗鱼手机端app中颜值分类的数据包baseURL = "http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset="offset = 0start_urls = [baseURL+str(offset)]def parse(self, response):# 1.将已经编码的json字符串解码为python对象(此处的Python对象时一个字典)# 1.data键对应的值是一个列表,列表中每一个元素又是一个字典data_list = json.loads(response.body)["data"]# 2.判断data是否为空(为空终止,不为空继续翻页)if not data_list:returnfor data in data_list:item = DouyuItem()item["game_name"] = data["game_name"]item["room_id"] = data["room_id"]item["vertical_src"] = data["vertical_src"]item["nickname"] = data["nickname"]item["message"] = data["game_name"]+data["room_id"]+data["nickname"]# 3.返回item对象给管道进行处理yield item# 4.JSON文件只能通过翻页来爬取,不能通过xpath()提取self.offset += 20# 5.将请求发回给引擎,引擎返回给调度器,调度器将请求入队列yield scrapy.Request(self.baseURL+str(self.offset), callback=self.parse)

pipelines.py

# -*- coding: utf-8 -*-
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
# 1.导入scrapy包中pipelines文件夹下images.py文件中ImagesPipeline类
import os
import scrapy
from Douyu.settings import IMAGES_STORE as images_store
from scrapy.pipelines.images import ImagesPipelineclass DouyuPipeline(ImagesPipeline):def get_media_requests(self, item, info):"""图片下载"""image_link = item["vertical_src"]# 2.管道将图片链接交给下载器下载yield scrapy.Request(image_link)def item_completed(self, results, item, info):"""图片重命名"""# 3.取出results中图片信息中:图片的存储路径# results是一个列表,列表中一个元组,元组中两个元素,第二个元素又是一个字典 / md5码# 推导式写法取值(最外层的列表相当于results列表)img_path = [x["path"] for ok, x in results if ok]# 4.为下载图片重命名# 4.os.rename(src_name, dst_name)os.rename(images_store+img_path[0], images_store + item["message"]+".jpg")return item

settings.py

# -*- coding: utf-8 -*-# Scrapy settings for Douyu project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.htmlBOT_NAME = 'Douyu'SPIDER_MODULES = ['Douyu.spiders']
NEWSPIDER_MODULE = 'Douyu.spiders'# 1.设置保存下载图片的路径images_store
# 1.另外一种路径方式IMAGES_STORE = "E:\\Python\\课程代码\\Douyu\\Douyu\\spiders\\"
IMAGES_STORE = "E:/Python/课程代码/Douyu/Douyu/spiders/"# Crawl responsibly by identifying yourself (and your website) on the user-agent
# 2.配置手机移动端user_agent
USER_AGENT = 'Mozilla/5.0 (iPhone 84; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 MQQBrowser/7.8.0 Mobile/14G60 Safari/8536.25 MttCustomUA/2 QBWebViewType/1 WKType/1'# Obey robots.txt rules
# 3.禁用robots协议(设置为False或者直接注释掉)
ROBOTSTXT_OBEY = False# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16# Disable cookies (enabled by default)
#COOKIES_ENABLED = False# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'Douyu.middlewares.DouyuSpiderMiddleware': 543,
#}# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'Douyu.middlewares.DouyuDownloaderMiddleware': 543,
#}# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
# 4.启用管道文件
ITEM_PIPELINES = {'Douyu.pipelines.DouyuPipeline': 300,
}# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

start.py

# -*- coding:utf-8 -*-
from scrapy import cmdlinecmdline.execute("scrapy crawl douyu".split())

3.图片爬取结果展示



这篇关于python3_scrapy爬取斗鱼手机端数据包(颜值模块主播照片)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/263309

相关文章

Python sys模块的使用及说明

《Pythonsys模块的使用及说明》Pythonsys模块是核心工具,用于解释器交互与运行时控制,涵盖命令行参数处理、路径修改、强制退出、I/O重定向、系统信息获取等功能,适用于脚本开发与调试,需... 目录python sys 模块详解常用功能与代码示例获取命令行参数修改模块搜索路径强制退出程序标准输入

Python pickle模块的使用指南

《Pythonpickle模块的使用指南》Pythonpickle模块用于对象序列化与反序列化,支持dump/load方法及自定义类,需注意安全风险,建议在受控环境中使用,适用于模型持久化、缓存及跨... 目录python pickle 模块详解基本序列化与反序列化直接序列化为字节流自定义对象的序列化安全注

python pymodbus模块的具体使用

《pythonpymodbus模块的具体使用》pymodbus是一个Python实现的Modbus协议库,支持TCP和RTU通信模式,支持读写线圈、离散输入、保持寄存器等数据类型,具有一定的参考价值... 目录一、详解1、 基础概念2、核心功能3、安装与设置4、使用示例5、 高级特性6、注意事项二、代码示例

录音功能在哪里? 电脑手机等设备打开录音功能的技巧

《录音功能在哪里?电脑手机等设备打开录音功能的技巧》很多时候我们需要使用录音功能,电脑和手机这些常用设备怎么使用录音功能呢?下面我们就来看看详细的教程... 我们在会议讨论、采访记录、课堂学习、灵感创作、法律取证、重要对话时,都可能有录音需求,便于留存关键信息。下面分享一下如何在电脑端和手机端上找到录音功能

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Nginx添加内置模块过程

《Nginx添加内置模块过程》文章指导如何检查并添加Nginx的with-http_gzip_static模块:确认该模块未默认安装后,需下载同版本源码重新编译,备份替换原有二进制文件,最后重启服务验... 目录1、查看Nginx已编辑的模块2、Nginx官网查看内置模块3、停止Nginx服务4、Nginx

python urllib模块使用操作方法

《pythonurllib模块使用操作方法》Python提供了多个库用于处理URL,常用的有urllib、requests和urlparse(Python3中为urllib.parse),下面是这些... 目录URL 处理库urllib 模块requests 库urlparse 和 urljoin编码和解码

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录

idea Maven Springboot多模块项目打包时90%的问题及解决方案

《ideaMavenSpringboot多模块项目打包时90%的问题及解决方案》:本文主要介绍ideaMavenSpringboot多模块项目打包时90%的问题及解决方案,具有很好的参考价值,... 目录1. 前言2. 问题3. 解决办法4. jar 包冲突总结1. 前言之所以写这篇文章是因为在使用Mav