为什么我在该代码中仍然存在相同的错误

时间:2018-09-24 10:58:01

标签: windows python-3.x urllib

我正在学习一个教程课程,我试图在python 3.7中创建一个Web应用程序,以查看文本文件中是否存在任何不良词

文本文件包含以下文本:

  

-休斯顿,我们有问题。 (阿波罗13号)

     

-妈妈总是说,生活就像一盒巧克力。您永远都不知道会得到什么。 (阿甘正传)

     

-您无法处理事实。 (几个好人)

     

-我什么都相信,我什么都不相信。 (在黑暗中射击)

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

我有那个错误:

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

那是什么错误,我应该怎么做才能消除它

  

urllib.error.HTTPError:HTTP错误400:错误的请求

我尝试添加此行代码:

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

然后:

connection = urllib.request.urlopen(url)

我知道了

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

我是python和Web应用程序编程的初学者,所以请帮助:(

2 个答案:

答案 0 :(得分:1)

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

看看问题#1695183

答案 1 :(得分:0)

我找到了解决方案 我认为发生这种情况是因为我没有在将字符串附加到url之前对其进行编码。

在python3中,我们应该先对“文本”执行以下操作,然后再将其附加到url:

text_to_check = urllib.parse.quote_plus(text)

Python2会是这样

  

(urllib在python3中被分解为较小的组件):

text_to_check = urllib.quote_plus(text_to_check)

这意味着,当将带有空格的字符串添加到url时,它将显示为“ Am + I + cursing%3F”,而不是“ Am I cursing?”。

完整的check_profanity()示例:

def check_profanity(text_to_check):
    text_to_check = urllib.parse.quote_plus(text_to_check)
    connection = urlopen(
        "http://www.wdylike.appspot.com/?q=" + text_to_check)
    output = connection.read()
print(output)
connection.close()