如何在Scrapy Splash请求中发送自定义标头?

Nad*_*era 5 python scrapy scrapy-splash splash-js-render

我的spider.py文件是这样的:

def start_requests(self):
    for url in self.start_urls:
        yield scrapy.Request(
            url,
            self.parse,
            headers={'My-Custom-Header':'Custom-Header-Content'},
            meta={
                'splash': {
                    'args': {
                        'html': 1,
                        'wait': 5,
                    },
                }
            },
        )
Run Code Online (Sandbox Code Playgroud)

而我的解析定义如下:

def parse(self, response):
    print(response.request.headers)
Run Code Online (Sandbox Code Playgroud)

当我运行Spider时,下面的行将作为标题打印:

{
    b'Content-Type': [b'application/json'], 
    b'Accept': [b'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'],
    b'Accept-Language': [b'en'], 
    b'User-Agent': [b'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36'], 
    b'Accept-Encoding': [b'gzip,deflate']
}
Run Code Online (Sandbox Code Playgroud)

如您所见,它没有我添加到Scrapy请求的自定义标头。

有人可以帮我添加此请求的自定义标头值吗?

提前致谢。

Jul*_* Š. 3

如果您希望splash 在对指定网址的请求中使用标头,那么您应该将标argshtml和一起添加到该部分wait

meta={
   'splash': {
        'args': {
            'html': 1,
            'wait': 5,
            'headers': {
                'My-Custom-Header': 'Custom-Header-Content',
            },
        },
    }
}
Run Code Online (Sandbox Code Playgroud)