from bs4 import BeautifulSoup soup = BeautifulSoup(resp.text,"html.parser") tags = soup("img") #爬取"img"标签 import os
for tag in tags: if tag.get("src") and tag.get('width') == "600": #"src"属性存在,且"width"属性值为600 img_src = tag.get("src") img_width = tag.get("width") filename = os.path.basename(img_src) with open(f"picture/{filename}","wb") as fout: fout.write(requests.get(img_src).content) print(img_src,filename,img_width)