Checkpoint 13.5.1.
Q-1: The _______ library is used to simplify sending and receiving data from a web page.
urllib
urllib
library.urllib
, you can treat a web page much like a file. You simply indicate which web page you would like to retrieve and urllib
handles all of the HTTP protocol and header details.urllib
is as follows:import urllib.request
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
for line in fhand:
print(line.decode().strip())
urllib.urlopen
, we can treat it like a file and read through it using a for
loop.urllib
code consumes the headers and only returns the data to us.romeo.txt
and compute the frequency of each word in the file as follows:import urllib.request, urllib.parse, urllib.error
fhand = urllib.request.urlopen('http://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)