正在获取“ urllib.error.HTTPError:HTTP错误400:请求错误”

时间:2018-08-10 10:02:43

标签: python python-3.x urllib

我一直在尝试检查文件中是否存在亵渎行为。我的代码显示了一个错误,提示“ 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

1 个答案:

答案 0 :(得分:0)

我没有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()
  

out:正确,错误,正确


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