NameError: name 'urllib' is not defined "

佐手、 提交于 2019-12-04 22:09:00

问题


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

回答1:


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

response = urlopen('...')



回答2:


You can also try in Python 3:

from six.moves import urllib

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



回答3:


Try pls:

from urllib.request import urlopen

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



回答4:


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)


来源:https://stackoverflow.com/questions/42314025/nameerror-name-urllib-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!