Skip to content

Instantly share code, notes, and snippets.

@tatianass
Created February 28, 2020 12:07
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 tatianass/29b77d4ed2f0d18a6d118360e6e176b1 to your computer and use it in GitHub Desktop.
Save tatianass/29b77d4ed2f0d18a6d118360e6e176b1 to your computer and use it in GitHub Desktop.
How to download images for websites and multiple links and save it in different folders.
from bs4 import BeautifulSoup, SoupStrainer
import requests
import os
import urllib
# Could be .png or other image type.
match_str = '.webp'
# Access many links in sequence.
for i in range(1, 99):
url = f"<http_link>{i}.html"
page = requests.get(url)
data = page.text
soup = BeautifulSoup(data)
save_path = f"{i:02d}/"
# Create dir to save images
os.mkdir(save_path)
for link in soup.find_all('img'):
src = link.get('src')
if match_str in src:
file_name = os.path.basename(src)[0:-1]
urllib.request.urlretrieve(src, os.path.join(save_path, file_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment