获得'urllib.error.HTTPError: HTTP Error 400: Bad Request'



我一直在尝试检查文件中是否有任何亵渎。我的代码出现一些错误,说">urllib.error.HTTPError:HTTP 错误 400:错误请求"

from urllib.request import urlopen

def open_file():
file = open('C://Users/black/Downloads/movie_quotes.txt')
contents = file.read()
print(contents)
file.close()
check_profanity(contents)

def check_profanity(input_file):
connection = urlopen('https://www.purgomalum.com/service/containsprofanity?text='+input_file)
output = connection.read()
print(output)
connection.close()

open_file()

这是文件: https://drive.google.com/open?id=1SU1I_cYC-S90eOJATs7APDJu7qnUhfs0

我没有你movie_quotes.txt文件,所以我只用一个列表测试你的代码。它运行没有问题。

from urllib.request import urlopen
def open_file():
file = ['shit', 'read', 'fuck']
for i in file:
check_profanity(i)
def check_profanity(input_file):
connection = urlopen('https://www.purgomalum.com/service/containsprofanity? 
text='+input_file)
output = connection.read()
print(output)
connection.close()
open_file()

出:真、假、真


所以我认为可能是你文件的问题,你可以测试你文件的读取输出

最新更新