In: Computer Science
Develop a Python program that brings together APIs and web scraping. Be creative. Pick something you are interested in - a hobby, a job, history, cooking, travel, sports, a foreign language, music, art, whatever you have some passion about.
Find an API that connects with your interest. Write a Python program that demonstrates the use of that api AND does some web scraping as well. It must include the ability to download images to your computer.
Submit the following to this Blackboard assignment:
1. your WELL DOCUMENTED source code file(s)
2. a word document that explains your program - what it does and how it does it
3. a Loom video link demonstrating what your program does - be sure to show off the images you download with your app.
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 17 14:26:02 2020
@author: Unknown
"""
######Sample python code begins
here##########################################
# Importing the needed modules for the application
import os
import json
import urllib.request
import random
def main():
# Debug line to indicate the start of the program
print("Starting in Main ....")
# Destination directory for storing the image from web
dest_dir = "Image"
#The API "https://dog.ceo/api/breeds/image/random" returns a random
DOG BREED image
response =
urllib.request.urlopen("https://dog.ceo/api/breeds/image/random")
#If the API call was a sucess get the dog breed URL else write an
error message
if response.getcode() == 200:
source = response.read()
data = json.loads(source)
else:
print('An error occurred while attempting to retrieve data from the
API.')
#From the API output get the URL for the DOG BREED image
dog_breed_url = data['message']
print("Dog Breed URL is ",dog_breed_url)
#Format the destination path to download the DOG BREED image
cur_dir = os.path.abspath(os.path.curdir)
odirpath = os.path.join(cur_dir, dest_dir)
if not os.path.exists(odirpath): os.mkdir(odirpath)
#Genarate a four digit random number for the file name
randomint = random.randint(0000,9999)
imgname = 'img' + str(randomint) + '.jpg'
imgpath = os.path.join(odirpath, imgname)
print("Destination Path the image downloaded is : ",imgpath)
#Download the image from web
urllib.request.urlretrieve(dog_breed_url, imgpath)
print("Program ended....")
# Calling the main function
if __name__ == '__main__':
main()
######Sample python code ends
here##########################################
The API URL - https://dog.ceo/api/breeds/image/random hosted the
internet returns an json file with the path from where the image
can be downloaded. The API returns an random image of a DOG
breed.
The Python program at a high level does the following things:
1. Makes a call to the API url
2.Checks the status of the API call.
3.A return code of 200 indicates a successful call to the API, in case of an uncessfull call the program prints an error message.
4. The API returns a json message with the location from where to download the image.
{"message":"https:\/\/images.dog.ceo\/breeds\/dalmatian\/cooper1.jpg","status":"success"}
5. JSON message is read to get the path of the jpg file for the download
6.The image is downloaded to the local machine in a destination folder named "Images".