Splash添加headers报错:'headers' must be either a JSON array of (name, value) pairs or a JSON object

背景

最近在测试爬虫的Splash方案,虽然已经实现了Selenium版方案,但是觉得配环境比较麻烦不适合分布式的场景,然后发现Splash是服务的形式提供一个接口访问,形式上类似走一层代理,对原有代码改动少,所以就开始测试过程,加代理已经实现,但是加headers时报错。

报错请求

请求render.html接口
请求方式:GET

URL="http://127.0.0.1:8050/render.html?url=https://httpbin.org/get&headers=[['User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0']]"
header={'Content-Type': 'application/json'}

报错内容

Splash添加headers报错:'headers' must be either a JSON array of (name, value) pairs or a JSON object_第1张图片

解决方法

  • 直到我仔细查看官方文档才发现headers参数只适用于post请求
    Splash添加headers报错:'headers' must be either a JSON array of (name, value) pairs or a JSON object_第2张图片

成功请求

请求render.html接口
请求方式:POST

URL='http://127.0.0.1:8050/render.html'
header={'Content-Type': 'application/json'}
data={"url":'https://httpbin.org/get','headers':[['User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0']]}

注意:需要对data进行json转换(使用json.dumps(data))

Splash添加headers报错:'headers' must be either a JSON array of (name, value) pairs or a JSON object_第3张图片
我的个人博客网站是:www.coderyyn.cn
上面会不定期分享有关爬虫、算法、环境搭建以及有趣的帖子
欢迎大家一起交流学习

转载请注明

你可能感兴趣的:(Python,爬虫,开发过程,Splash,渲染引擎,爬虫)