[英]HTTP Error when using urllib.request
我正在嘗試進行褻瀆性檢查。 到目前為止我寫的代碼是
import urllib.request
def read_text ():
file = open (r"C:\Users\Kashif\Downloads\abc.txt")
file_print = file.read ()
print (file_print)
file.close ()
check_profanity (file_print)
def check_profanity (file_print):
connection = urllib.request.urlopen ("http://www.purgomalum.com/service/containsprofanity?text="+file_print)
output = connection.read ()
print ("The Output is "+output)
connection.close ()
read_text ()
但是我得到下面的錯誤
urllib.error.HTTPError:HTTP錯誤400:錯誤的請求
我不知道我要怎么做。
我正在使用Python 3.6.1
您收到的HTTP錯誤通常表明您向服務器請求數據的方式出現了問題。 根據HTTP規范 :
400錯誤的要求
由於語法格式錯誤,服務器無法理解該請求。 客戶端不應不加修改地重復請求
在您的示例中,具體來說,問題似乎出在您在URL中發送的數據缺少URL編碼。 你應該嘗試使用方法quote_plus
從的urllib.parse模塊,使您的要求可以接受的:
from urllib.parse import quote_plus
...
encoded_file_print = quote_plus(file_print)
url = "http://www.purgomalum.com/service/containsprofanity?text=" + encoded_file_print
connection = urllib.request.urlopen(url)
如果這不起作用,則問題可能出在文件的內容上。 您可以先通過一個簡單的示例進行嘗試,以驗證腳本是否正常運行,然后再嘗試使用文件的內容。
除了上述內容之外,您的代碼還存在其他一些問題:
file.close ()
或def read_text ():
等。 output = connection.read().decode('utf-8')
調用方法的方式會創建循環依賴項。 read_text
調用check_profanity
,在結束通話read_text
調用check_profanity
等刪除多余的方法調用,只需使用return
返回一個方法的輸出:
content = read_text() has_profanity = check_profanity(content) print("has profanity? %s" % has_profanity)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.