2016-12-28 5 views
0

以下は、ファイル内の呪いの単語をチェックするための私のpythonコードです。 しかし、私はコンパイラがエラーを表示している理由を見つけることができません:module 'urllib' has no attribute 'urlopen'私のコード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() 
+1

を使用する必要がありながら、あなたは、urllib2を使用する必要がありますか? –

答えて

2

Python 3では、urllibは、複数のモジュールを収集するパッケージになりました。 urlopen() is now a part of urllib.request module:次に

from urllib.request import urlopen 

それを使用して:

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

助けていただきありがとうございます。私はpython 2.xのオンラインソースをチェックしてその間違いを犯しました – Borris

1

まあ、urlliburlopenメソッドを持っていないため。 Python2では

のPython 3にあなたのpythonのバージョンを使用しているurllib.request

関連する問題