python之多线程图片抓取
站点目标: https://www.2717.com/ 编写脚本: import os import threading import time import requests from bs4 import BeautifulSoup headers = { 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', } def do_request(url, tries=3): # 添加重试机制 i = 0 while i < tries: try: res = requests.get(url, timeout=5) return res except requests.exceptions.RequestException as e: i += 1 print('request error.', e) return False def download_local(url, img_file): ''' 下载图片到本地 :param url: 远程图片地址 :param img_file: 本地存储地址 ''' res = do_request(url) if