import threading, requests
import time, re
from bs4 import BeautifulSoup as bs
url ='https:///photo/list/'
headers ={"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"}
r = requests.get(url, headers=headers)
html = r.text
html = bs(html,'lxml')
imgs = html.find_all(attrs={"data-original":True})
img_url_list =[]for tag in imgs:
img_url_list.append({"img_name": tag["alt"],"img_url": tag["data-original"]})defget_img(img_name, img_url):
r = requests.get(img_url)print(r.status_code)
file_name = img_name +'.jpg'print(file_name)withopen(file_name,'wb')as f:
f.write(r.content)# 创建子线程
thread_list =[]for item in img_url_list:
img_name, img_url = item["img_name"], item["img_url"]print(img_url, img_name)
t = threading.Thread(target=get_img, args=(img_name, img_url))
thread_list.append(t)for t in thread_list:
t.start()for t in thread_list:
t.join()