如何使用python3检查亵渎

2024-05-23 16:03:27 发布

您现在位置: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(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的反馈:

^{pr2}$

Tags: oftextimportclosereadparserequestdef
2条回答

在www.wdyl.com网站不起作用了。在

你可以用你喜欢的代替: http://www.wdylike.appspot.com/?q=foo

这是我的工作代码:

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

另一种方法是:

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

相关问题 更多 >