【发布时间】: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 似乎可能认为我是一个回合,这就是原因。但是我在我的代码中添加了什么来解决这个问题?我知道你可能不知道,但我认为这就是问题所在。