我的代码 urlopen 函数有什么问题?
What is wrong with my code urlopen function?
下面是我的 python 代码,用于检查文件中的诅咒词。
但我无法找到编译器显示错误的原因:module 'urllib' has no attribute 'urlopen'
.
import urllib
def read_txt():
quote = open("c:\read.txt") #for opening the file
content = quote.read() #for reading content in a file
print(content)
quote.close() #for closing the file
check_profanity(content)
def check_profanity(text):
connection = urllib.urlopen("https://www.wdylike.appspot.com/?q=" + text)
ouput = connection.read()
print(output)
connection.close()
read_txt()
在Python3,urllib
is now a package that collects multiple modules. urlopen()
is now a part of urllib.request
module:
from urllib.request import urlopen
然后使用它:
connection = urlopen("https://www.wdylike.appspot.com/?q=" + text)
嗯,因为 urllib
没有 urlopen
方法。
在Python2中你应该使用urllib2
,而在Python3中你应该使用urllib.request
下面是我的 python 代码,用于检查文件中的诅咒词。
但我无法找到编译器显示错误的原因:module 'urllib' has no attribute 'urlopen'
.
import urllib
def read_txt():
quote = open("c:\read.txt") #for opening the file
content = quote.read() #for reading content in a file
print(content)
quote.close() #for closing the file
check_profanity(content)
def check_profanity(text):
connection = urllib.urlopen("https://www.wdylike.appspot.com/?q=" + text)
ouput = connection.read()
print(output)
connection.close()
read_txt()
在Python3,urllib
is now a package that collects multiple modules. urlopen()
is now a part of urllib.request
module:
from urllib.request import urlopen
然后使用它:
connection = urlopen("https://www.wdylike.appspot.com/?q=" + text)
嗯,因为 urllib
没有 urlopen
方法。
在Python2中你应该使用urllib2
,而在Python3中你应该使用urllib.request