Skip to content

Instantly share code, notes, and snippets.

@rustymyers
Last active April 23, 2020 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rustymyers/1b53dd25d2cc455c02d72619a0a11d55 to your computer and use it in GitHub Desktop.
Save rustymyers/1b53dd25d2cc455c02d72619a0a11d55 to your computer and use it in GitHub Desktop.
#!python3
#--------------------------------------------------------------------------------------------------
#-- Download Star Wars Images
#--------------------------------------------------------------------------------------------------
# Program : getURLImages.py
# To Complie : n/a
#
# Purpose : Download images from a web page.
# Speifically https://www.starwars.com/news/star-wars-backgrounds
#
# Called By :
# Calls :
#
# Author : Rusty Myers <rzm102@psu.edu>
# Based Upon :
#
# Note :
#
# Revisions :
# 2020-04-23 <rzm102> Initial Version
#
# Version : 1.0
#--------------------------------------------------------------------------------------------------
import os, re
import requests, shutil
from bs4 import BeautifulSoup
from pathlib import Path
counter = 1
imageFolder = "./Star Wars Images/"
lastName = ""
def downloadURL(url, name):
"""downloadURL: Download URL to name"""
resp = requests.get(url, stream=True)
local_file = open(imageFolder + name + ".jpg", "wb")
resp.raw.decode_content = True
shutil.copyfileobj(resp.raw, local_file)
del resp
Path(imageFolder).mkdir(parents=True, exist_ok=True)
r = requests.get('https://www.starwars.com/news/star-wars-backgrounds')
soup = BeautifulSoup(r.text, "html.parser")
for link in soup.findAll('img'):
image_url = link.get('src')
image_name = link.get('alt').replace(":"," -")
if ".jpg" in image_url and "backgrounds" in image_url and "Mosaic" not in image_name:
if image_name == lastName:
counter += 1
else:
counter = 1
lastName = image_name
downloadURL(image_url,image_name + " {0}".format(str(counter)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment