Skip to main content

Section 13.19 Write Code Exercises

Activity 13.19.1.

Complete the following code that retrieves the file โ€˜romeo.txtโ€™ from. Make changes to line 4 and 5.
Solution.
import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode(),end='')

    mysock.close()

Activity 13.19.2.

Complete the following code to extract an image โ€˜cover3.jpgโ€™ from the URL โ€˜http://data.pr4e.org/cover3.jpgโ€™ and host โ€˜data.pr4e.orgโ€™. There are 5 empty spaces.

Activity 13.19.3.

Complete the following code that retrieves the text from โ€˜http://data.pr4e.org/clown.txtโ€™, prints it and also prints the frequency of each word.
Solution.
import urllib.request

fhand = urllib.request.urlopen('http://data.pr4e.org/clown.txt')
for line in fhand:
    words = line.decode().strip()
    print(words)
    for word in words:
        counts[word] = counts.get(word, 0) + 1
print(counts)

Activity 13.19.5.

Write a program to store image file from โ€˜http://data.pr4e.org/cover.jpgโ€™ to your disk.
Solution.
import urllib.request, urllib.parse, urllib.error

img = urllib.request.urlopen('http://data.pr4e.org/cover.jpg').read()
fhand = open('cover.jpg', 'wb')
fhand.write(img)
fhand.close()

Activity 13.19.6.

Complete the following program to extract all url from the webpage using regex.

Activity 13.19.7.

Write a program that retrives a txt file from โ€˜https://www.gutenberg.org/files/1342/1342-0.txtโ€™ in several blocks of 100,000 characters, joins them and saves as โ€˜prideandprejudice.txtโ€™ to disk and prints number of characters.
Solution.
import urllib.request, urllib.parse, urllib.error

txt = urllib.request.urlopen('https://www.gutenberg.org/files/1342/1342-0.txt')
fhand = open('prideandprejudice.txt', 'wb')
size = 0
while True:
    info = txt.read(100000)
    if len(info) < 1: break
    size = size + len(info)
    fhand.write(info)

print(size, 'characters copied.')
fhand.close()

Activity 13.19.8.

Write a program that retrives a txt file from โ€˜https://www.gutenberg.org/files/16/16-0.txtโ€™ in several blocks of 100,000 characters, joins them and saves as โ€˜peterpan.txtโ€™ to disk and prints number of characters.

Activity 13.19.9.

Complete the following code to print all the image sources from the webpage. Use โ€˜imgโ€™ and โ€˜srcโ€™ as tags.
Solution.
import requests
from bs4 import BeautifulSoup

url = "https://www.nytimes.com/"
resp = requests.get(url)
soup = BeautifulSoup(resp.content, 'html.parser')

tags = soup('img')
for tag in tags:
    print(tag.get('src', None))

Activity 13.19.10.

Write code that extracts data from several parts of the โ€˜aโ€™ tag from โ€œhttp://www.dr-chuck.com/page1.htmโ€ using BeautifulSoup and html.parser and print the tag, href, contents as well as all the attributes.
You have attempted of activities on this page.