Created
February 28, 2020 12:07
-
-
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.
This file contains 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
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