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的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python3 pip终端出现错误解决的方法详解

《python3pip终端出现错误解决的方法详解》这篇文章主要为大家详细介绍了python3pip如果在终端出现错误该如何解决,文中的示例方法讲解详细,感兴趣的小伙伴可以跟随小编一起了解一下... 目录前言一、查看是否已安装pip二、查看是否添加至环境变量1.查看环境变量是http://www.cppcns

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

Python datetime 模块概述及应用场景

《Pythondatetime模块概述及应用场景》Python的datetime模块是标准库中用于处理日期和时间的核心模块,本文给大家介绍Pythondatetime模块概述及应用场景,感兴趣的朋... 目录一、python datetime 模块概述二、datetime 模块核心类解析三、日期时间格式化与

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络