2016-12-28 1 views
0

Unten ist mein Python-Code, um Schimpfwörter in der Datei zu überprüfen. Aber ich bin nicht in der Lage zu finden, warum der Compiler Fehler zeigt: module 'urllib' has no attribute 'urlopen'.Was ist falsch mit meiner Code-URL-Funktion?

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

Welche Version von Python verwenden sollten, verwenden Sie? –

Antwort

2

In Python 3 urllib ist nun ein Paket, das mehrere Module sammelt. urlopen() is now a part of urllib.request module:

from urllib.request import urlopen 

mit ihm dann:

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

Danke, Sir, dass Sie mir geholfen haben. Ich habe diesen Fehler gemacht, indem ich die Online-Quelle für Python 2.x überprüft habe – Borris

1

Nun, weil urllib keine urlopen Methode.

In Python2 sollten Sie urllib2 verwenden, während in Python 3 Sie urllib.request