【问题标题】:How to use Splash with python-requests?如何将 Splash 与 python 请求一起使用?
【发布时间】:2016-09-01 07:10:24
【问题描述】:

我想在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

一起使用

【问题讨论】:

    标签: python-2.7 scrapy python-requests splash-screen scrapyjs


    【解决方案1】:

    meta 是 Scrapy Request 特有的,python-requests' request 没有 meta 参数,因此出现 TypeError 异常。

    要将 Splash 与 python-requests 一起使用,请阅读 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_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 还有allows POST-ed requests to the endpoint:

    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})
    

    【讨论】:

    • 但是我应该发送一个 POST 请求
    • 你的意思是Splash应该向目标网站发出POST请求吗?如果是,有http_method and body arguments/render.html 可用
    • 我需要在我的请求 POST 中设置“等待”
    猜你喜欢
    • 1970-01-01
    • 2021-01-11
    • 2023-03-23
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 2013-08-20
    相关资源
    最近更新 更多