1. 程式人生 > >爬蟲——Scrapy框架案例一:手機APP抓包

爬蟲——Scrapy框架案例一:手機APP抓包

debug domain hone targe allow topic document more ebs

以爬取鬥魚直播上的信息為例:

URL地址:http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset=0

爬取字段:房間ID、房間名、圖片鏈接、存儲在本地的圖片路徑、昵稱、在線人數、城市

1.items.py

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy

class DouyuspiderItem(scrapy.Item):
    # define the fields for your item here like:
    # 房間ID
    room_id = scrapy.Field()
    # 房間名
    room_name = scrapy.Field()
    # 圖片鏈接
    vertical_src = scrapy.Field()
    # 存儲圖片的本地地址
    image_path = scrapy.Field()
    # 昵稱
    nickname = scrapy.Field()
    # 在線人數
    online = scrapy.Field()
    # 城市
    anchor_city = scrapy.Field()

2.spiders/douyu.py

# -*- coding: utf-8 -*-
import scrapy
from douyuSpider.items import DouyuspiderItem
import json

class DouyuSpider(scrapy.Spider):
    name = ‘douyu‘
    allowed_domains = [‘capi.douyucdn.cn‘]
    url = ‘http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset=‘
    offset = 0
    start_urls = [url + str(offset)]

    def parse(self, response):
        # 是否爬取下一頁的標記
        next_flag = False
        data = json.loads(response.text)["data"]
        for each in data:
            item = DouyuspiderItem()
            # 房間ID
            item[‘room_id‘] = each["room_id"]
            # 房間名
            item[‘room_name‘] = each["room_name"]
            # 圖片鏈接
            item[‘vertical_src‘] = each["vertical_src"]
            # 昵稱
            item[‘nickname‘] = each["nickname"]
            # 在線人數
            item[‘online‘] = each["online"]
            # 城市
            item[‘anchor_city‘] = each["anchor_city"]

            next_flag = True

            yield item

        # 判斷是否繼續爬取下一頁
        if next_flag:
            self.offset += 20
            yield scrapy.Request(self.url + str(self.offset), callback = self.parse)

3.pipelines.py

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don‘t forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import json
import scrapy
from scrapy.pipelines.images import ImagesPipeline
from scrapy.utils.project import get_project_settings
import os

# 存儲信息的json文件中間件
class DouyuspiderPipeline(object):
    def __init__(self):
        self.file = open("鬥魚.json", "w", encoding = "utf-8")
        self.first_flag = True

    def process_item(self, item, spider):
        if self.first_flag:
            self.first_flag = False
            content = "[\n" + json.dumps(dict(item), ensure_ascii = False)
        else:
            content = ",\n" + json.dumps(dict(item), ensure_ascii = False)
        self.file.write(content)

        return item

    def close_spider(self, spider):
        self.file.write("\n]")
        self.file.close()

# 下載圖片的中間件
class ImagesPipeline(ImagesPipeline):
    IMAGES_STORE = get_project_settings().get("IMAGES_STORE")

    def get_media_requests(self, item, info):
        image_url = item["vertical_src"]

        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]

        os.rename(self.IMAGES_STORE + "/" + image_path[0], self.IMAGES_STORE + "/" + item["nickname"] + ".jpg")
        item["image_path"] = self.IMAGES_STORE + item["nickname"] + ".jpg"

        return item

4.settings.py

# -*- coding: utf-8 -*-

# Scrapy settings for douyuSpider 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

BOT_NAME = ‘douyuSpider‘

SPIDER_MODULES = [‘douyuSpider.spiders‘]
NEWSPIDER_MODULE = ‘douyuSpider.spiders‘


# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = ‘douyuSpider (+http://www.yourdomain.com)‘

# Obey robots.txt rules
ROBOTSTXT_OBEY = True

# 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 = {
   ‘Accept‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8‘,
   ‘User-Agent‘: ‘DYZB/2.290 (iPhone; iOS 9.3.4; Scale/2.00)‘
#   ‘Accept-Language‘: ‘en‘,
}

# Enable or disable spider middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    ‘douyuSpider.middlewares.DouyuspiderSpiderMiddleware‘: 543,
#}

# Enable or disable downloader middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    ‘douyuSpider.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 = {
    ‘douyuSpider.pipelines.DouyuspiderPipeline‘: 300,
    ‘douyuSpider.pipelines.ImagesPipeline‘: 200,
}

# Images 的存放位置,之後會在pipelines.py裏調用
IMAGES_STORE = "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框架案例一:手機APP抓包