import requests from bs4 import BeautifulSoup import threading import time headers = { "User-Agent": 'Mozilla/5.0 (Windows NT 6.1; WOW64) ' 'AppleWebKit/537.36 (KHTML, like Gecko)' 'Chrome/90.0.4430.212 Safari/537.36' } def download(url): start_time = time.time() # 记录开始时间 response = requests.get(url, headers=headers).text soup = BeautifulSoup(response, features='lxml') src = soup.find_all('img') imagesrc = soup.find_all('img', width="100") for s in imagesrc: with open("{}.jpg".format(s.get('alt')), 'wb') as file: image = requests.get(s.get('src')).content file.write(image) print("正在下载" + s.get('alt') + '.jpg') end_time = time.time() # 记录结束时间 print("线程 {} 运行时间为:{} 秒".format(threading.current_thread().name, end_time - start_time)) threads = [] for x in range(10): url = "https://movie.douban.com/top250?start={}&filter=".format(x * 25) thread = threading.Thread(target=download, args=(url,), name="Thread-{}".format(x+1)) threads.append(thread) thread.start() for thread in threads: thread.join()改为单线程
时间: 2023-06-24 12:07:58 浏览: 123
import requests from bs4 import BeautifulSoup import time headers = { "User-Agent": 'Mozilla/5.0 (Windows NT 6.1; WOW64) ' 'AppleWebKit/537.36 (KHTML, like Gecko)' 'Chrome/90.0.4430.212 Safari/537.36' } def download(url): start_time = time.time() # 记录开始时间 response = requests.get(url, headers=headers).text soup = BeautifulSoup(response, features='lxml') src = soup.find_all('img') imagesrc = soup.find_all('img', width="100") for s in imagesrc: with open("{}.jpg".format(s.get('alt')), 'wb') as file: image = requests.get(s.get('src')).content file.write(image) print("正在下载" + s.get('alt') + '.jpg') end_time = time.time() # 记录结束时间 print("运行时间为:{} 秒".format(end_time - start_time)) url_list = [] for x in range(10): url = "https://movie.douban.com/top250?start={}&filter=".format(x * 25) url_list.append(url) for url in url_list: download(url)
阅读全文