Python NameError: 名称 'urllib' 未定义“

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42314025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:32:02  来源:igfitidea点击:

NameError: name 'urllib' is not defined "

pythonpython-2.7urllib

提问by Amit Mishra

CODE:

代码:

import networkx as net
from urllib.request import urlopen
def read_lj_friends(g, name):
# fetch the friend-list from LiveJournal
response=urllib.urlopen('http://www.livejournal.com/misc/fdata.bml?user='+name)

ERROR:

错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'urllib' is not defined

回答by Daniel Roseman

You've imported urlopendirectly, so you should refer to it like that rather than via urllib:

您已urlopen直接导入,因此您应该像这样引用它,而不是通过urllib:

response = urlopen('...')

回答by gogasca

You can also try in Python 3:

你也可以在 Python 3 中尝试:

from six.moves import urllib

temp_file, _ = urllib.request.urlretrieve(url)

回答by Tolgahan üZüN

Try pls:

请尝试:

from urllib.request import urlopen

html = urlopen("http://www.google.com/")
print(html.read) # Content 

回答by Banjali

For your case:

对于您的情况:

import networkx as net
from urllib.request import urlopen
def read_lj_friends(g, name):
# fetch the friend-list from LiveJournal
response=urlopen('http://www.livejournal.com/misc/fdata.bml?user='+name)