如何使用Splash与python请求?

par*_*rik 2 splash-screen scrapy python-2.7 python-requests scrapyjs

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

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

但我有这个错误

TypeError: request() got an unexpected keyword argument 'meta'
Run Code Online (Sandbox Code Playgroud)

我知道这与scrapy.Request有关,但我想用于请求

pau*_*rth 8

meta是Scrapy -specific Requestpython-requests'请求没有meta参数,因此是TypeError例外.

要将Splash与python请求一起使用,请阅读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})
Run Code Online (Sandbox Code Playgroud)

如果您希望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})
Run Code Online (Sandbox Code Playgroud)

/render.html允许对端点的POST-ed请求:

Splash通过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})
Run Code Online (Sandbox Code Playgroud)