3

我想在请求中使用 splash ,像这样

requests.post(myUrl,headers=myHeaders, data=payload, meta={
                                        'splash': {
                                            'endpoint': 'render.html',
                                            'args': {'wait': 1}
                                            }
                                        })

但我有这个错误

TypeError: request() got an unexpected keyword argument 'meta'

我知道这适用于scrapy.Request但我想使用requests

4

1 回答 1

11

meta是 Scrapy特定的Request,并且python-requests 的请求没有meta参数,因此是TypeError例外。

要将 Splash 与 python-requests 一起使用,请阅读HTTP API 文档,尤其是render.html因为这似乎是您想要使用的。

您需要向/render.html端点发送 GET 请求,并将目标 URL 和wait参数作为查询参数传递,例如:

import requests
requests.get('http://localhost:8050/render.html',
             params={'url': 'http://www.example.com', 'wait': 2})

如果您希望 Splash 向目标网站发出 POST 请求,请使用http_methodbody参数:

import requests
requests.get('http://localhost:8050/render.html',
              params={'url': 'http://httpbin.org/post',
                      'http_method': 'POST',
                      'body': 'a=b',
                      'wait': 2})

/render.html允许向端点发送 POST 请求

飞溅是通过 HTTP API 控制的。对于下面的所有端点,参数可以作为 GET 参数发送或编码为 JSON 并与Content-Type: application/json标头一起发布。

但默认方法仍然是 GET。要对目标网站进行 POST,您仍然需要包含一个http_method参数:

import requests

requests.post('http://localhost:8050/render.html',
              json={'url': 'http://httpbin.org/post',
                    'http_method': 'POST',
                    'body': 'a=b',
                    'wait': 2})
于 2016-05-06T10:42:08.407 回答