scrapy实战3利用fiddler对手机app进行抓包爬虫图片下载(重写ImagesPipeline):

时间:2023-03-09 19:56:46
scrapy实战3利用fiddler对手机app进行抓包爬虫图片下载(重写ImagesPipeline):

关于fiddler的使用方法参考(http://jingyan.baidu.com/article/03b2f78c7b6bb05ea237aed2.html)

本案例爬取斗鱼 app

先利用fiddler分析抓包json数据如下图

scrapy实战3利用fiddler对手机app进行抓包爬虫图片下载(重写ImagesPipeline):

通过分析发现变化的只有offset  确定item字段 开始编写代码

items.py

 import scrapy

 class DouyuItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
# 存储照片的名字
nickname=scrapy.Field()
# 照片的url路径
imagelink=scrapy.Field()
# 照片保存在本地的路径
imagepath=scrapy.Field()

spider/Douyu.py

 import scrapy
import json
from douyu.items import DouyuItem class DouyuSpider(scrapy.Spider):
name = "Douyu"
allowed_domains = ["capi.douyucdn.cn"]
offset=0
url="http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset="
start_urls = [url+str(offset)] def parse(self, response):
# 将从json里获取的数据转换成python对象 data段数据集合 response.text获取内容
data=json.loads(response.text)["data"]
for each in data:
item=DouyuItem()
item["nickname"]=each["nickname"]
item["imagelink"]=each["vertical_src"]
yield item
self.offset+=100
yield scrapy.Request(self.url+str(self.offset),callback=self.parse)

pipelines.py

import scrapy
from scrapy.pipelines.images import ImagesPipeline
from douyu.items import DouyuItem
from scrapy.utils.project import get_project_settings
import os
class DouyuPipeline(object):
def process_item(self, item, spider):
return item
class ImagesPipelines(ImagesPipeline): IMAGES_STORE=get_project_settings().get("IMAGES_STORE")
def get_media_requests(self, item, info):
# get_media_requests的作用就是为每一个图片链接生成一个Request对象,这个方法的输出将作为item_completed的输入中的results,results是一个元组,
# 每个元组包括(success, imageinfoorfailure)。如果success=true,imageinfoor_failure是一个字典,包括url/path/checksum三个key。
image_url=item["imagelink"]
yield scrapy.Request(image_url)
def item_completed(self, results, item, info):
# 固定写法,获取图片路径,同时判断这个路径是否正确,如果正确,就放到 image_path里,ImagesPipeline源码剖析可见
image_path=[x["path"] for ok,x in results if ok]
print(image_path)
os.rename(self.IMAGES_STORE+'/'+image_path[0],self.IMAGES_STORE+"/"+item["nickname"]+".jpg")
item["imagepath"]=self.IMAGES_STORE+"/"+item["nickname"]

settints.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:
#
# http://doc.scrapy.org/en/latest/topics/settings.html
# http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
# http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
import os
BOT_NAME = 'douyu' SPIDER_MODULES = ['douyu.spiders']
NEWSPIDER_MODULE = 'douyu.spiders' # Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'douyu (+http://www.yourdomain.com)' # Obey robots.txt rules
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 http://scrapy.readthedocs.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 = {
"USER_AGENT" : 'DYZB/2.290 (iPhone; iOS 9.3.4; Scale/2.00)'
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Accept-Language': 'en',
}
# Enable or disable spider middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'douyu.middlewares.DouyuSpiderMiddleware': 543,
#} # Enable or disable downloader middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'douyu.middlewares.MyCustomDownloaderMiddleware': 543,
#} # Enable or disable extensions
# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#} # Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
# 'scrapy.pipelines.images.ImagesPipeline': 1,
'douyu.pipelines.ImagesPipelines': 300,
}
# Images 的存放位置,之后会在pipelines.py里调用
project_dir=os.path.abspath(os.path.dirname(__file__))
IMAGES_STORE=os.path.join(project_dir,'images') #images可以随便取名
# Enable and configure the AutoThrottle extension (disabled by default)
# See http://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 http://scrapy.readthedocs.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'

数据:

scrapy实战3利用fiddler对手机app进行抓包爬虫图片下载(重写ImagesPipeline):