基于urllib实现定时循环请求URL并保存图片的技术问询
Hey there! Let's figure out how to make your image download script run every 5 seconds without a hitch. I've got two straightforward approaches for you, depending on your needs:
Approach 1: Basic Loop with time.sleep
This is the simplest solution—no extra libraries needed, perfect for quick and dirty recurring tasks. The key here is to wrap your download logic in a function, then loop it with a 5-second pause between runs. Also, we'll make sure each saved image has a unique filename so you don't overwrite previous ones.
Here's the modified code:
import urllib.request import time from datetime import datetime def download_face_image(): # Fix the incomplete User-Agent string (servers might block partial ones!) opener = urllib.request.URLopener() opener.addheader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36') # Generate a unique filename using timestamp to avoid overwrites timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") save_path = f'/Users/barony/Desktop/Face Paint/Public/gettyimages-2637237_{timestamp}.jpg' try: _, headers = opener.retrieve("https://thispersondoesnotexist.com/image", save_path) print(f"Image saved successfully to: {save_path}") except Exception as e: print(f"Oops, failed to download image: {str(e)}") # Run the function repeatedly every 5 seconds while True: download_face_image() time.sleep(5)
Quick Notes:
- Unique Filenames: The timestamp ensures each download gets its own file—no more accidentally overwriting your images.
- Error Handling: The
try-exceptblock catches network issues or server errors, so your script won't crash abruptly. - Fixed User-Agent: I completed your User-Agent string; many servers reject requests with partial or missing UA info.
Approach 2: Using the schedule Library (More Elegant)
If you want a cleaner way to manage recurring tasks (or might need to adjust the schedule later, like running hourly instead), the schedule library is a great choice. First, install it via pip:
pip install schedule
Then use this code:
import urllib.request import schedule import time from datetime import datetime def download_face_image(): opener = urllib.request.URLopener() opener.addheader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36') timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") save_path = f'/Users/barony/Desktop/Face Paint/Public/gettyimages-2637237_{timestamp}.jpg' try: _, headers = opener.retrieve("https://thispersondoesnotexist.com/image", save_path) print(f"Image saved successfully to: {save_path}") except Exception as e: print(f"Oops, failed to download image: {str(e)}") # Set up the recurring task: run every 5 seconds schedule.every(5).seconds.do(download_face_image) # Keep the script running to execute scheduled tasks while True: schedule.run_pending() time.sleep(1) # Check for pending tasks every 1 second (light on CPU)
Why This Is Better:
- Readable Schedule: The
schedule.every(5).seconds.do(...)line makes it crystal clear when the task runs. - Flexibility: Later, you can easily change it to
schedule.every().hour.do(...)or evenschedule.every().day.at("14:30").do(...)without rewriting the loop logic. - Resource Friendly: The
time.sleep(1)in the loop means the script doesn't hog CPU cycles checking for tasks nonstop.
General Tips:
- To stop either script, just press
Ctrl+Cin your terminal. - If you're running this for a long time, consider adding logging instead of print statements for better debugging.
- Some servers might rate-limit frequent requests—if you start getting errors, try increasing the interval from 5 seconds to something longer.
内容的提问来源于stack exchange,提问作者dasnaar

