downloader-middleware

介绍本库中自带的常用 DOWNLOADER_MIDDLEWARES 中间件。

需要使用本库中的配置,需要在 spider 中修改如下,此为前提:

from ayugespidertools.spiders import AyuSpider


# 当前 spider 要继承 AyuSpider
class DemoOneSpider(AyuSpider):
    ...

1. 随机UA

使用 fake_useragent 库中的 ua 信息,在每次发送请求时将随机取 ua 信息,将比较常用的 ua 标识的权重设置高一点,这里是根据 fake_useragent 库中的打印信息来规划权重的,即类型最多的 ua 其权重也就越高。

1.1. 使用方法

只需激活 DOWNLOADER_MIDDLEWARES 对应的配置即可。

custom_settings = {
    "DOWNLOADER_MIDDLEWARES": {
        # 随机请求头
        "ayugespidertools.middlewares.RandomRequestUaMiddleware": 400,
    },
}

若想查看是否正常运行,只需查看其 scrapydebug 日志,或在 spider 中打印 response 信息然后查看其信息即可。

2. 代理

2.1. 动态隧道代理

本库以快代理为例,其各个代理种类的使用方法大致相同

2.1.1. 使用方法

激活 DOWNLOADER_MIDDLEWARES 中的动态代理配置。

custom_settings = {
    "DOWNLOADER_MIDDLEWARES": {
        # 动态隧道代理激活
        "ayugespidertools.middlewares.DynamicProxyDownloaderMiddleware": 125,
    },
}

需要修改此项目中的 VIT 文件夹下的 .conf 对应的配置信息。

[KDL_DYNAMIC_PROXY]
PROXY=o668.kdltps.com:15818
USERNAME=***
PASSWORD=***

然后即可正常运行。

2.2. 独享代理

2.2.1. 使用方法

激活 DOWNLOADER_MIDDLEWARES 中的独享代理配置。

custom_settings = {
    "DOWNLOADER_MIDDLEWARES": {
        # 独享代理激活
        "ayugespidertools.middlewares.ExclusiveProxyDownloaderMiddleware": 125,
    },
}

需要修改此项目中的 VIT 文件夹下的 .conf 对应的配置信息。

[kdl_exclusive_proxy]
proxy=http://kps.kdlapi.com/api/getkps?orderid=***&num=100&format=json
username=***
password=***
index=1

注:index 为在有多个独享代理时取的代理对应的索引值。

3. 发送请求方式改为 aiohttp

3.1. 使用方法

激活 DOWNLOADER_MIDDLEWARES 中的对应配置。

custom_settings = {
    "TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor",
    "DOWNLOADER_MIDDLEWARES": {
        # 将 scrapy Request 替换为 aiohttp 方式
        "ayugespidertools.middlewares.AiohttpDownloaderMiddleware": 543,
    },
    # scrapy Request 替换为 aiohttp 的配置示例
    "AIOHTTP_CONFIG": {
        "proxy": "http://127.0.0.1:7890",
        "sleep": 1,
        # 同时连接的总数
        "limit": 100,
        # 同时连接到一台主机的数量
        "limit_per_host": 0,
        "retry_times": 3,
        "verify_ssl": False,
        "allow_redirects": False,
    },
    # aiohttp 的超时时间也用这个配置,统一管理且不允许修改
    "DOWNLOAD_TIMEOUT": 25,
}

注:

  • TWISTED_REACTOR 的配置在本库的 settings 中就默认打开的,这里配置是为了演示,不用再次配置的;

  • LOCAL_AIOHTTP_CONFIG 为 aiohttp 的全局配置,一般可用来配置 proxytimeoutretry_timessleep 等全局的参数的;

然后需要构造 AiohttpRequestAiohttpFormRequest 请求对象,具体的 aiohttp 参数在 args 中配置:

具体使用习惯和方式看个人选择,如下:

# 方式一: 通过 args 参数传值
yield AiohttpRequest(
    url="http://httpbin.org/get?get_args=1",
    callback=self.parse_get_fir,
    args=AiohttpRequestArgs(
        method="GET",
        headers={
            "Cookie": "headers_ck_key1=ck; headers_ck_key2=ck",
        },
        cookies={
            "ck_key": "ck",
        },
    ),
)

# 方式二:使用 scrapy 传统方式传值
yield AiohttpRequest(
    url="http://httpbin.org/get?get_args=1",
    callback=self.parse_get_fir,
    headers={
        "Cookie": "headers_ck_key1=ck; headers_ck_key2=ck",
    },
    cookies={
        "ck_key": "ck",
    },
)

# 同样,发送 post 也可以选择两种方式
# 测试 POST 请求示例一 - normal
post_data = {"post_key1": "post_value1", "post_key2": "post_value2"}
yield AiohttpRequest(
    url="http://httpbin.org/post",
    method="POST",
    callback=self.parse_post_fir,
    headers={
        "Cookie": "headers_ck_key=ck; headers_ck_key2=ck",
    },
    body=json.dumps(post_data),
    cookies={
        "ck_key": "ck",
    },
)
# 测试 POST 请求示例一 - aiohttp args
yield AiohttpRequest(
    url="http://httpbin.org/post",
    callback=self.parse_post_fir,
    args=AiohttpRequestArgs(
        method="POST",
        headers={
            "Cookie": "headers_ck_key=ck; headers_ck_key2=ck",
        },
        cookies={
            "ck_key": "ck",
        },
        data=json.dumps(post_data),
    ),
)

# 测试 POST 请求示例二 - normal
yield AiohttpFormRequest(
    url="http://httpbin.org/post",
    headers={
        "Cookie": "headers_ck_key=ck; headers_ck_key2=ck",
    },
    cookies={
        "ck_key": "ck",
    },
    formdata=post_data,
    callback=self.parse_post_sec,
)
# 测试 POST 请求示例二 - aiohttp args
yield AiohttpFormRequest(
    url="http://httpbin.org/post",
    callback=self.parse_post_sec,
    args=AiohttpRequestArgs(
        method="POST",
        headers={
            "Cookie": "headers_ck_key=ck; headers_ck_key2=ck",
        },
        cookies={
            "ck_key": "ck",
        },
        data=post_data,
    ),
)