【问题标题】:How to send post request in websites?如何在网站上发送 post 请求?
【发布时间】:2019-07-21 14:04:31
【问题描述】:

我最近开始研究 Python 中的 requests 模块,遇到了使用 data={something} 向网站发送数据的 POST 方法。所以,我很好奇并尝试向 google.com 发送帖子,并想看看我的如果我想搜索'hello',则返回结果。我总是收到405 错误。我想知道为什么会收到此错误,甚至有没有办法向用户必须填写一些数据的任何网站发送 POST 请求?

我知道我也可以使用GET,但我特别想使用 POST。

我在下面使用了这段代码。

import requests

data={'q':'hello'}

headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0'}

p_resp=requests.post('https://www.google.com/',headers=headers,data=data)

print(p_resp.text)

我收到了405 error The request method POST is inappropriate for the URL

【问题讨论】:

    标签: python-3.x post python-requests


    【解决方案1】:

    实际上如果你想在Google中搜索你需要使用GET方法而不是POST。 这是一个搜索单词hello的示例:

    import requests
    url = 'https://www.google.com/complete/search'
    req = requests.get(url, params={'pql': 'hello'})
    print(req.status_code)
    

    输出:

    200
    

    但是,如果您使用 POST 方法,您将拥有:

    req = requests.post(url, params={'pql': 'hello'})
    print(req.status_code)
    

    输出:

    405
    

    如果您搜索200405 状态码之间的区别,您会看到:

    The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.

    The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the request method is known by the server but is not supported by the target resource.

    因此,在这种情况下,您需要有另一个支持POST 方法的目标才能进行测试。一个微不足道的目标是您自己的支持POST 的本地服务器。

    更多详情见here

    【讨论】:

    • 如果我需要在其他网站(如 amazon 或 youtube)上搜索内容怎么办。我是否也必须对那些使用 get 。此外,这些网站不使用 POST 而是使用 GET 是否有特定原因。因为 POST 在 debian.org 上为我工作。
    • @DJayGurung 后面的开发者是否支持所有CURD 方法或其中一些方法,这取决于服务本身。
    • 那么,如果这两个都不起作用,除了 GET 和 POST 还有其他方法吗?
    • @DJayGurung 查看我在答案中添加的链接。
    猜你喜欢
    • 2015-09-20
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2021-02-28
    • 2012-07-04
    相关资源
    最近更新 更多