I would like to import urllib to use the function 'request'. However, I encountered an error when trying to do so. I tried pip install urllib but still had the same error. I am using Python 3.6. Really appreciate any help.
i do import urllib.request using this code:
import urllib.request, urllib.parse, urllib.error fhand = urllib.request.urlopen('data.pr4e.org/romeo.txt') counts = dict() for line in fhand: words = line.decode().split() for word in words: counts[word] = counts.get(word, 0) + 1 print(counts)
but it gives me this error: ModuleNotFoundError: No Module named 'urllib.parse'; 'urllib' is not a package
here is a screenshot for the error
The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.
You need to use from urllib. request import urlopen , also I suggest you use the with statement while opening a connection. @BradleyD. Freeman-Bain: you can't have a with -statement without the following block.
urllib
is a standard library, you do not have to install it. Simply import urllib
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With