此代码成功运行..
import urllib.request
def profanity():
connection = urllib.request.urlopen('http://www.wdylike.appspot.com/?q='+'bal')
output = connection.read()
print(output)
connection.close()
profanity()
但是我想运行下面的代码给我带来了问题。但我想传递从本地txt文件中读取的数据并将此数据传递给profanity()函数。该怎么办?
import urllib.request
def read_file():
qoutes=open(r"C:\Python34\profanity.txt")
a=qoutes.read()
profanity(a)
qoutes.close()
def profanity(b):
connection = urllib.request.urlopen('http://www.wdylike.appspot.com/?q='+b)
output = connection.read()
print(output)
connection.close()
##profanity()
read_file()
错误日志:
Traceback (most recent call last):
File "C:\Python34\check_profanity.py", line 18, in <module>
read_file()
File "C:\Python34\check_profanity.py", line 8, in read_file
profanity(a)
File "C:\Python34\check_profanity.py", line 12, in profanity
connection = urllib.request.urlopen('http://www.wdylike.appspot.com/?q='+b)
File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 462, in open
req = meth(req)
File "C:\Python34\lib\urllib\request.py", line 1106, in do_request_
raise URLError('no host given')
urllib.error.URLError: <urlopen error no host given>