爬虫——Scrapy框架案例一:手机APP抓包

以爬取斗鱼直播上的信息为例:

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‘
时间: 2024-10-28 11:25:34

爬虫——Scrapy框架案例一:手机APP抓包的相关文章

jmeter编写脚本之手机app抓包

pc端抓包及常用请求脚本编写,点击打开链接. 首先大家应该清楚手机app原理 据我了解,现在市面上收大概分两种, 一类是手游,用游戏引擎开发的客户端,这类我还未涉猎,不敢高谈: 二类是网站app,一般采用html5+css3作为app前端,实际上app就好比一个浏览器(其实也是浏览器内核),只要知道了主页登陆地址,我们就可以在模拟器上运行app了. 这里推荐使用chrome的开发者工具,具备手机浏览器模拟功能,还可以选择多种手机类型. 按F12开启抓包之旅(Windows系统) 如下图示:

Fiddler安卓手机APP抓包

一,设置Fiddler:打开Fiddler, Tools-> Fiddler Options (配置完后记得要重启Fiddler)选中"Decrpt HTTPS traffic", Fiddler就可以截获HTTPS请求 选中"Allow remote computers to connect". 是允许别的机器把HTTP/HTTPS请求发送到Fiddler上来.记住这个端口号是:8888 二,设置安卓手机:win+r,输入cmd,输入ipconfig,获取电

爬虫——Scrapy框架案例二:阳光问政平台

阳光热线问政平台 URL地址:http://wz.sun0769.com/index.php/question/questionType?type=4&page= 爬取字段:帖子的编号.投诉类型.帖子的标题.帖子的URL地址.部门.状态.网友.时间. 1.items.py # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # http://doc.sc

想开发网页爬虫,发现被反爬了?想对 App 抓包,发现数据被加密了?不要担心,这里可以为你解决。

全面超越Appium,使用Airtest超快速开发App爬虫 想开发网页爬虫,发现被反爬了?想对 App 抓包,发现数据被加密了?不要担心,使用 Airtest 开发 App 爬虫,只要人眼能看到,你就能抓到,最快只需要2分钟,兼容 Unity3D.Cocos2dx-*.Android 原生 App.iOS App.Windows Mobile……. Airtest是网易开发的手机UI界面自动化测试工具,它原本的目的是通过所见即所得,截图点击等等功能,简化手机App图形界面测试代码编写工作. 安

python爬虫scrapy框架——人工识别登录知乎倒立文字验证码和数字英文验证码(2)

操作环境:python3 在上一文中python爬虫scrapy框架--人工识别登录知乎倒立文字验证码和数字英文验证码(1)我们已经介绍了用Requests库来登录知乎,本文如果看不懂可以先看之前的文章便于理解 本文将介绍如何用scrapy来登录知乎. 不多说,直接上代码: import scrapy import re import json class ZhihuSpider(scrapy.Spider): name = 'zhihu' allowed_domains = ['www.zhi

fiddler 抓取手机app请求包

今天心血来潮,也不知道怎么了,想着抓抓我们公司手机app的包看看,研究研究我们公司的接口,哎,我们api文档,我自己抓包看看吧.工具选择fiddler,理由免费,用着也舒服,手机设备 iPhone6 ,app这里不介绍了. 第一步,设置代理, 打开fiddler,选择tool>Options>Connections ,设置端口号,勾选 Allow_remote computers to connect.然后ok.这样就完成了代理的设置.重启fiddler 查看电脑ip 第二步 手机链接代理服务

charles抓手机app的包的操作步骤

以下是本人整理的charles抓手机app的包的操作步骤,如有疑问或建议之类的可以私发我邮箱:谢谢 1.先设置代理服务器的端口号,如下图所示 2.选择在移动设备上安装 Charles 根证书. 3.会弹出一个提示框,提示框的ID和端口号是要在手机上输入的代理设置(此端口号是第一步设置的) 4.把手机的ip填写到如下页面(手机IP可以在手机设置无线网查看) 5.进入手机设置界面,手机局域网设置,然后打开手机的浏览器,输入charlesproxy.com/getssl 会弹出如下界面. 6.这样就可

使用Fiddler抓取手机APP数据包--360WIFI

使用Fiddler抓取手机APP流量--360WIFI 操作步骤:1.打开Fiddler,Tools-Fiddler Options-Connections,勾选Allow remote computers to connect,端口为8888:2.防火墙开放端口8888:2.在电脑上查看360wifi无线网卡IP地址,运行命令ipconfig /all,查看无线局域网适配器的IP信息192.168.1.100:3.手机wifi中设置代理为步骤2中的IP地址192.168.1.100,端口为步骤

安装爬虫 scrapy 框架前提条件

安装爬虫 scrapy 框架前提条件 (不然 会 报错) 1 pip install pypiwin32 原文地址:https://www.cnblogs.com/xmdykf/p/11374484.html