【问题标题】:How to use pastebin API with python?[specific error]如何在 python 中使用 pastebin API?[具体错误]
【发布时间】:2021-08-13 09:33:48
【问题描述】:

我正在尝试将 pastebin api 与文档一起使用:python https://pastebin.com/doc_api。使用 urllib 库:https://docs.python.org/3/library/urllib.html.

import urllib.request
import urllib.parse

def main():

    def pastebinner():
        site = 'https://pastebin.com/api/api_post.php'
        dev_key = 
        code = "12345678910, test"      
        our_data = urllib.parse.urlencode({"api_dev_key": dev_key, "api_option": "paste", "api_paste_code": code})  
        our_data = our_data.encode()                    
        resp = urllib.request.urlopen(site, our_data)
        print(resp.read())
        
    pastebinner()

if __name__ == "__main__":
    main()

这是我得到的错误:

文件 "C:\Program 文件\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", 第 214 行,在 urlopen 返回 opener.open(url, data, timeout) 文件 "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", 第 523 行,打开 response = meth(req, response) 文件 "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", 第 632 行,在 http_response 响应 = self.parent.error(文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py”, 第 561 行,错误 返回 self._call_chain(*args) 文件 "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", 第 494 行,在 _call_chain 结果 = func(*args) 文件 "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", 第 641 行,在 http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 422: Unprocessable entity

关于出现此错误的原因有什么想法吗?

bump:我还是不知道,请帮忙。 凹凸2::v

【问题讨论】:

  • @Nearoo 为什么这会使请求无法解析? api_paste_code 应该是粘贴的内容吧?来自文档:Creating A New Paste, [Required Parameters] Include all the following POST parameters when you request the url: 1. api_dev_key - which is your unique API Developers Key. 2. api_option - set as paste, this will indicate you want to create a new paste. 3. api_paste_code - this is the text that will be written inside your paste. Leaving any of these parameters out will result in an error.
  • 对 :) 没有意义,抱歉。
  • @Nearoo 有一个对用户更友好的 python 库,但我真的不想使用它,因为 urllib 是标准库的一部分。根据我所做的一些阅读,pastebin 似乎可能认为我是一个回合,这就是原因。但是我在我的代码中添加了什么来解决这个问题?我知道你可能不知道,但我认为这就是问题所在。

标签: python pastebin


【解决方案1】:

您正在使用urllib.request.urlopen(site, our_data),这是一个 HTTP GET(default 用于 urllib 中的任何内容)。您需要改为执行 HTTP POST。 Obligatory w3 link

请注意以下代码未经测试

import urllib.request
import urllib.parse


def main():

    def pastebinner():
        site = 'https://pastebin.com/api/api_post.php'
        dev_key = 'APIKEYGOESHERE'
        code = "12345678910, test"
        our_data = urllib.parse.urlencode({"api_dev_key": dev_key, "api_option": "paste", "api_paste_code": code})
        our_data = our_data.encode()
        request = urllib.request.Request(site, method='POST')
        resp = urllib.request.urlopen(request, our_data)
        print(resp.read())

    pastebinner()


if __name__ == "__main__":
    main()

这个错误非常无用。我的意思是,为什么不返回 teapot response 呢?

【讨论】:

    【解决方案2】:

    将其留在这里以防其他人遇到此问题。对此不是 100% 确定,稍后将测试 DONT USE URLLIB2 USE httplib2。我相信这会解决您的问题。

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 2011-11-26
      • 2015-10-24
      • 2014-08-23
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多