theinterpreter17
theinterpreter17

Reputation: 11

Getting 'urllib.error.HTTPError: HTTP Error 400: Bad Request'

I have been trying to check if there is any profanity in a file. My code comes up with some error saying 'urllib.error.HTTPError: HTTP Error 400: Bad Request'

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

here's the file: https://drive.google.com/open?id=1SU1I_cYC-S90eOJATs7APDJu7qnUhfs0

Upvotes: 0

Views: 837

Answers (1)

popcorn
popcorn

Reputation: 21

I don't have you movie_quotes.txt file, So I test your code just with a list. It runs no problem.

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: true, false, true


So I think may be your file's problem, your can test what your file's read output

Upvotes: 1

Related Questions