Skip to content

Instantly share code, notes, and snippets.

@rberrelleza
Created April 11, 2015 02:56
Show Gist options
  • Save rberrelleza/9e7cbc5fd19889c71a1e to your computer and use it in GitHub Desktop.
Save rberrelleza/9e7cbc5fd19889c71a1e to your computer and use it in GitHub Desktop.
Screen scrapper+pngcrusher exercise to
import shutil
import requests
from bs4 import BeautifulSoup
from tinypng import shrink_file
requests.packages.urllib3.disable_warnings()
response = requests.get("YOUR_URL")
soup = BeautifulSoup(response.text, 'html.parser')
images = soup.find_all('img')
counter = 1
api_key = "YOUR_TINYPNG_KEY"
for image in images:
image_source = image.get("src")
if image_source.endswith(".png"):
response = requests.get(image_source, stream=True)
image_name = "results/" + str(counter) + ".png"
with open(image_name, "wb") as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)
compressed_image_name = "compressed/" + str(counter) + ".png"
shrink_info = shrink_file(
image_name,
api_key=api_key,
out_filepath=compressed_image_name)
counter = counter + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment