名称错误:未定义名称“urllib”

2024-04-16 21:56:18 发布

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

代码:

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)

错误:

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

Tags: 代码namefromimportnetworkxreadnetrequest
3条回答

您也可以在Python 3中尝试:

from six.moves import urllib

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

请尝试:

from urllib.request import urlopen

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

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

response = urlopen('...')

相关问题 更多 >