Question

In: Computer Science

Develop a Python program that brings together APIs and web scraping. Be creative. Pick something you...

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.

Solutions

Expert Solution

# -*- 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".


Related Solutions

In this assignment, you will develop a simple Web server in Python that is capable of...
In this assignment, you will develop a simple Web server in Python that is capable of processing only one request. Specifically, your Web server will (i) create a connection socket when contacted by a client (browser); (ii) receive the HTTP request from this connection; (iii) parse the request to determine the specific file being requested; (iv) get the requested file from the server’s file system; (v) create an HTTP response message consisting of the requested file preceded by header lines;...
Develop a python program to autonomously operate an adeept tank robot to pick up an object...
Develop a python program to autonomously operate an adeept tank robot to pick up an object placed randomly in an arena.
You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program...
You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program accepts the lengths of three (3) sides of a triangle as input from a player. The program output should indicate whether the triangle is a right triangle, an acute triangle, or an obtuse triangle. 1.Tips to help you for this assignment: Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input. 2. Validating the triangle. That...
Python It's time to put everything together!  This week you will write a program that mimics an...
Python It's time to put everything together!  This week you will write a program that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. I leave the bulk of design up to you (hint - think A LOT about it before you start programming!!) but it should include all of the following: The basics - Add items to a cart until the user...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You should generate B complement signal (flip all the bits of Input B,
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
Posting together because they are related. Python 1. Create a program to count the number of...
Posting together because they are related. Python 1. Create a program to count the number of occurrences of each letter of the alphabet in a text file, and print out the total for each letter (if greater than 0). Ignore numbers, punctuation, and special characters. Make sure to treat capital and lowercase letters as occurrences of the same letter. The text file (sample_text.txt) is attached to the assignment. Example: For the text "My dog's name is Winston." The results would...
Develop a python program to - Create a 2D 10x10 numpy array and fill it with...
Develop a python program to - Create a 2D 10x10 numpy array and fill it with the random numbers between 1 and 9 (including 0 and 9). - Assume that you are located at (0,0) (upper-left corner) and want to move to (9,9) (lower-right corner) step by step. In each step, you can only move right or down in the array. - Develop a function to simulate random movement from (0,0) to (9,9) and return sum of all cell values...
You will create an interest web based on something you like to do. Please follow the...
You will create an interest web based on something you like to do. Please follow the step-by-step directions below. 1) Make a list of the things/activities you liked to do as a child. 2) Make a list of the things/activities you like to do as an adult. 3) Find one thing in common from both of your lists - it should be an activity that you liked doing as a child and still like doing today. Take that one activity...
TheMathGame Develop a program in PYTHON that will teach children the basic math facts of addition....
TheMathGame Develop a program in PYTHON that will teach children the basic math facts of addition. The program generates random math problems for students to answer. User statistics need to be kept in RAM as the user is playing the game and recorded in a text file so that they may be loaded back into the program when the student returns to play the game again. In addition, the youngster should be allowed to see how (s)he is doing at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT