簡體   English   中英

如何使用 pastebin API 和 python?[具體錯誤]

[英]How to use pastebin API with python?[specific error]

我正在嘗試將 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 Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py”,第 523 行,打開響應 = meth(req, response) 文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation .Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 632, in http_response response = self.parent.error( File "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__lib\qburllib\nfra800 request.py”,第 494 行,在 _call_ 中鏈結果 = func(*args) 文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py”,第 641 行,在 http_error_default 中引發 HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP 錯誤 422: 無法處理的實體

關於出現此錯誤的原因有什么想法嗎?

凹凸:我還是不知道請幫忙。 凹凸2::v

您正在使用urllib.request.urlopen(site, our_data)這是一個 HTTP GET ( 默認為 urllib 中的任何內容)。 您需要改為執行 HTTP POST。 強制性 w3 鏈接

請注意,下面的代碼未經測試

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

該錯誤非常無益。 我的意思是,為什么不返回一個茶壺響應呢?

把這個留在這里,以防其他人遇到這個問題。 不是 100% 確定這一點,稍后將測試 DONT USE URLLIB2 USE httplib2。 我相信這會解決你的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM