我的代码urlopen函数有什么问题?

2024-05-23 13:55:21 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的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()

Tags: thetxtforclosereaddefcheckcontent
2条回答

因为urllib没有urlopen方法。你知道吗

在Python2中应该使用urllib2,而在python3中应该使用urllib.request

在python3中,^{}现在是一个收集多个模块的包。^{} is now a part of ^{} module

from urllib.request import urlopen

然后使用它:

connection = urlopen("https://www.wdylike.appspot.com/?q=" + text)

相关问题 更多 >