NameError: name 'urllib' is not defined \"

2020-06-17 07:38发布

问题:

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)