如何在 python 请求中使用 Splash?
How to use Splash with python-requests?
我想在 requests 中使用 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
一起使用
meta
是 Scrapy Request
特定的,python-requests' request 没有 meta
参数,因此 TypeError
异常。
要将 Splash 与 python-请求一起使用,请阅读 HTTP API docs, especially on 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_method
和 body
参数:
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
也 allows POST-ed requests to the endpoint:
Splash is controlled via HTTP API. For all endpoints below parameters may be sent either as GET arguments or encoded to JSON and POSTed with Content-Type: application/json
header.
但默认方法仍然是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})
我想在 requests 中使用 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
一起使用meta
是 Scrapy Request
特定的,python-requests' request 没有 meta
参数,因此 TypeError
异常。
要将 Splash 与 python-请求一起使用,请阅读 HTTP API docs, especially on 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_method
和 body
参数:
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
也 allows POST-ed requests to the endpoint:
Splash is controlled via HTTP API. For all endpoints below parameters may be sent either as GET arguments or encoded to JSON and POSTed with
Content-Type: application/json
header.
但默认方法仍然是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})