首页 > 解决方案 > Python亵渎检查器错误

问题描述

亵渎检查器似乎正在工作,但没有返回(响应:false)或响应:true)

下面看一下代码:

import urllib.request

    def read_text():
        quotes = open("/Users/sellis/Desktop/movie_quotes.rtf")
        contents_of_file = quotes.read()
        print(contents_of_file)
        quotes.close()
        check_profanity(contents_of_file)

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

    read_text()

当它运行时,它会出现:

{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
{\fonttbl\f0\fmodern\fcharset0 Courier;}
{\colortbl;\red255\green255\blue255;\red0\green0\blue0;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\sl280\partightenfactor0

\f0\fs24 \cf2 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 -- 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)\
}
Traceback (most recent call last):
  File "/Users/sellis/Desktop/check_profanity.py", line 16, in <module>
    read_text()
  File "/Users/sellis/Desktop/check_profanity.py", line 8, in read_text
    check_profanity(contents_of_file)
  File "/Users/sellis/Desktop/check_profanity.py", line 11, in check_profanity
    connection = urllib.request.urlopen(" http://www.wdylike.com/profanity?q"+text_to_check)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 570, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 505: HTTP Version Not Supported

代码运行后,它应该显示 response: false 或 response: true 取决于在阅读代码后函数是否发现有一个 cuss 词

标签: python

解决方案


推荐阅读