如何使用python3检查亵渎
请帮助我更正此代码的问题:如何使用python3检查亵渎,python,Python,请帮助我更正此代码的问题: import urllib.request import urllib.parse def read_text(): quotes= open("C:\\Users\\no1\\Desktop\\movie_quotes.txt") content_of_file=quotes.read() quotes.close() check_profanity(content_of_file) def check_profanity(te
import urllib.request
import urllib.parse
def read_text():
quotes= open("C:\\Users\\no1\\Desktop\\movie_quotes.txt")
content_of_file=quotes.read()
quotes.close()
check_profanity(content_of_file)
def check_profanity(text_to_check):
a=urllib.parse.urlencode(text_to_check)
a=a.encode('utf-8')
connection=urllib.request.urlopen("http://www.wdyl.com/profanity?q=",a)
output=connection.read()
print(output)
connection.close()
read_text()
我想检查亵渎行为,但代码不起作用
以下是python的反馈:
Traceback (most recent call last):
File "C:\Python34\lib\urllib\parse.py", line 760, in urlencode
raise TypeError
TypeError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\no1\Desktop\check profanity.py", line 19, in <module>
read_text()
File "C:\Users\no1\Desktop\check profanity.py", line 9, in read_text
check_profanity(content_of_file)
File "C:\Users\no1\Desktop\check profanity.py", line 12, in check_profanity
a=urllib.parse.urlencode(text_to_check)
File "C:\Python34\lib\urllib\parse.py", line 768, in urlencode
"or mapping object").with_traceback(tb)
File "C:\Python34\lib\urllib\parse.py", line 760, in urlencode
raise TypeError
TypeError: not a valid non-string sequence or mapping object
回溯(最近一次呼叫最后一次):
文件“C:\Python34\lib\urllib\parse.py”,第760行,urlencode
提高打字错误
打字错误
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\no1\Desktop\check-profanity.py”,第19行,在
阅读文本()
文件“C:\Users\no1\Desktop\check-profanity.py”,第9行,只读文本
检查亵渎(文件内容)
文件“C:\Users\no1\Desktop\check profanity.py”,第12行,在check\u profanity中
a=urllib.parse.urlencode(文本到检查)
文件“C:\Python34\lib\urllib\parse.py”,第768行,urlencode
“或映射对象”)。带有回溯(tb)
文件“C:\Python34\lib\urllib\parse.py”,第760行,urlencode
提高打字错误
TypeError:不是有效的非字符串序列或映射对象
这里是另一种类似的方式:
from django.utils.encoding import smart_str
import urllib
def check_profanity(to_check):
try:
connection = urllib.urlopen('http://www.wdyl.com/profanity?q='\
+ smart_str(to_check))
output = connection.read()
connection.close()
if 'true' in output:
return True
else:
return False
except:
print "An error occurred!"
return
www.wdyl.com不再工作了 您可以使用您喜欢的内容: 这是我的工作代码:
import urllib.request
import urllib.parse
def read_text():
quotes = open("movie_quotes.txt") # Returns a `file` object
contents_of_file = quotes.read()
print(contents_of_file)
quotes.close()
check_profanity(contents_of_file)
def check_profanity(text_to_check):
encoded_text = urllib.parse.quote(text_to_check, 'utf-8')
address = "http://www.wdylike.appspot.com/?q="+encoded_text
print(address)
connection = urllib.request.urlopen(address)
output = connection.read()
print(output)
connection.close()
read_text()
pip安装-U咒语
?