NameError: name 'urllib' is not defined "
Asked Answered
S

5

15

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
Subway answered 18/2, 2017 at 10:38 Comment(0)
O
11

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

response = urlopen('...')
Osteoporosis answered 18/2, 2017 at 10:44 Comment(0)
P
6

You can also try in Python 3:

from six.moves import urllib

temp_file, _ = urllib.request.urlretrieve(url)
Papandreou answered 1/12, 2018 at 7:47 Comment(0)
E
6

Just put import urllib at the top of your code

Everglades answered 13/8, 2020 at 9:34 Comment(0)
S
2

Try pls:

from urllib.request import urlopen

html = urlopen("http://www.google.com/")
print(html.read) # Content 
Shaun answered 18/2, 2017 at 10:51 Comment(2)
urllib is part of the standard library. You don't need to install urllib3 which is a different library. Also, urlopen returns a file-like object, not the html.Coloratura
Complicated, sorry. I'm fixing it. Thank you for warning.Circe
D
0

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)
Degenerate answered 17/7, 2018 at 6:53 Comment(2)
Welcome to SO. Your post contains exactly the same code as in the original question, except for the stray back tick at the end. See stackoverflow.com/help/how-to-answer for guidance.Convexoconvex
Hey, thanks. The only change required in code is removing urllib in the last line of code. Please look once.Degenerate

© 2022 - 2024 — McMap. All rights reserved.