Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Testing API requests here
- import requests # Web scraping/API access module
- """
- Used API - https://random-d.uk/api?format=json
- Returning Random Duck photo
- Both links use Version 2:
- https://random-d.uk/api
- https://random-d.uk/api/v2
- """
- """Below request pulls a duck photo link from the Random Duck API
- 'URL' contains random duck image
- 'Response' collects data from URL
- response.json() asks for json format of data from link, creating usable dictionary
- 'duck_photo_link' Specifies dict key (url), which will pull dict value (Random duck photo URL)"""
- # Grabs random duck photo or gif, converts to JSON dict format, then pulls the link by singling out the key ('url')
- URL = 'https://random-d.uk/api/random'
- response = requests.get(URL)
- duck_photo_link = response.json()["url"]
- print(duck_photo_link)
- # Creates extension variable for .jpg or .gif photos
- if duck_photo_link[-4][-1] == "jpg":
- extension = '.jpg'
- else:
- extension = '.gif'
- # Asks user to name file, attaches extension
- filename_input = input("Name your file: ")
- duck_filename = filename_input + extension
- # Downloads duck photo to computer using requests module
- scraped_image = requests.get(duck_photo_link)
- with open(duck_filename, 'wb') as f:
- f.write(scraped_image.content)
- # This puts photo in same location as the .py file is located, fix later
- print("Download Successful:", duck_filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement