美文网首页
Scrapy发起Http 2.0请求

Scrapy发起Http 2.0请求

作者: 会爬虫的小蟒蛇 | 来源:发表于2022-08-17 17:53 被阅读0次

https://match.yuanrenxue.com/api/match/17?page=1 网站为例
注:https://www.hgy209.com/xmgs这个网站也是h2

1.png

这里可以看到他所使用的协议是http2

如果你使用http1协议去请求就会报错

局部配置http2

import scrapy


class TestSpider(scrapy.Spider):
    name = 'test'
    custom_settings = {
        "DOWNLOAD_HANDLERS": {
            'https': 'scrapy.core.downloader.handlers.http2.H2DownloadHandler',
        }
    }

    def start_requests(self):
        yield scrapy.Request(
            url="https://match.yuanrenxue.com/api/match/17?page=1",
            callback=self.parse,
        )

    def parse(self, response):
        print(response)

这样就可以轻松抓到通过http2传输的数据

这里演示是使用的Spider局部添加的方法

全局配置http2

如果想要全局添加 可以直接修改Settings.py

DOWNLOAD_HANDLERS = {
     'https': 'scrapy.core.downloader.handlers.http2.H2DownloadHandler',
}

相关文章

网友评论

      本文标题:Scrapy发起Http 2.0请求

      本文链接:https://www.haomeiwen.com/subject/rcnegrtx.html