首页 > 解决方案 > why i keep having the same error in that code

问题描述

i'm following a tutorial course i'm trying to make a web application in python 3.7 that see if there is any bad words in a text file

the text file contains the following text :

-- Houston, we have a problem. (Apollo 13)

-- Mama always said, life is like a box of chocolates. You never know what you are going to get. (Forrest Gump)

-- You cant handle the truth. (A Few Good Men)

-- I believe everything and I believe nothing. (A Shot in the Dark)

import urllib.request

def read_text():
    quotes = open (r'C:\Users\M\Desktop\TEMP FILES\movie_quotes.txt')
    content_of_file = quotes.read()
    print(content_of_file)
    quotes.close()
    check_profanity(content_of_file)

def check_profanity(text):

    connection = urllib.request.urlopen( 'http://www.wdylike.appspot.com/?q='+text)
   output = connection.read()
   print(output)
   connection.close()

read_text()

and i have that error :

Traceback (most recent call last):
File "C:\Users\Mosa Abbas\Desktop\TEMP FILES\ipnd-starter-code-master\ipnd-starter-code-master\stage_3\lesson_3.3_classes\c_profanity_editor\check_profanity.py", line 32, in <module>
read_text()
File "C:\Users\Mosa Abbas\Desktop\TEMP FILES\ipnd-starter-code-master\ipnd-starter-code-master\stage_3\lesson_3.3_classes\c_profanity_editor\check_profanity.py", line 23, in read_text
check_profanity(content_of_file)
File "C:\Users\Mosa Abbas\Desktop\TEMP FILES\ipnd-starter-code-master\ipnd-starter-code-master\stage_3\lesson_3.3_classes\c_profanity_editor\check_profanity.py", line 27, in check_profanity
connection = urllib.request.urlopen( 'http://www.wdylike.appspot.com/?q='+text)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

what is that error and what i should do to get rid of it

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

i'v tried to add this line of code :

url = 'http://www.wdylike.appspot.com/?q='+urllib.parse.quote(text, safe = '/')

and then:

connection = urllib.request.urlopen(url)

and i got

 raise RemoteDisconnected("Remote end closed connection without"
 http.client.RemoteDisconnected: Remote end closed connection without response

i'm a beginner in python and programming web applications so please help :(

标签: windowspython-3.xurllib

解决方案


通过 GET 方法发送 HTTP 请求时,需要对 GET 变量进行转义。因此,您可能应该在将文本附加到 URL 之前对其进行转义。

看看问题#1695183


推荐阅读