Created
April 11, 2015 02:56
-
-
Save rberrelleza/9e7cbc5fd19889c71a1e to your computer and use it in GitHub Desktop.
Screen scrapper+pngcrusher exercise to
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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