首页 > 解决方案 > NameError:名称'urllib'未定义

问题描述

大家好,现在我知道堆栈溢出有解决方案我只是不明白,这是我的代码

enter code here
import urllib.request import urlopen

def read_text():
quotes = open(r"C:\Users\LEON\Desktop\python\Swear_words.txt")
contents_of_file = quotes.read()
print(contents_of_file)
quotes.close()
check_profanity(contents_of_file)


def check_profanity(text_to_check):
connection = 
urllib.urlopen("http://www.wdylike.appspot.com/q=s"+text_to_check)
output = connection.read()


read_text() 

错误 NameError: 名称 'urllib' 未定义

标签: python

解决方案


import 语句本身的语法不正确。它应该/可能是

from urllib.request import urlopen

那么最后第三行不需要urllib前缀

urlopen("http://www.wdylike.appspot.com/q=s"+text_to_check)

推荐阅读