(Jun-23-2020, 11:50 AM)Blue Dog Wrote: I have the script in its own folder so the maps should be saved in the folder that the script is in. Maybe a while loop will work better. I am just lost on saving the images.The loop is already done in my code,so inside this loop can use
os.path.basename
to get correct names of images when save.Then to get the content(bytes) of images need also to open links with Requests,then can save.
Here also put in a progress bar with tqdm.
import requests from bs4 import BeautifulSoup import os # pip install tqdm from tqdm import tqdm url = 'http://legacy.lib.utexas.edu/maps/topo/indiana/' response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') maps = soup.select_one('#actualcontent > ul') map_link = maps.find_all('a')[:-1] for link in tqdm(map_link): img_name = os.path.basename(link.get('href')) #print(img_name) img = requests.get(link.get('href')) with open(img_name, 'wb') as f_out: f_out.write(img.content)