import os
#导入发送请求模块:
import requests
#导入解析文本模块:
from bs4 import BeautifulSoup
#返回上一级目录:
base_path = os.path.dirname(os.path.abspath(__file__))
#路径和图片文件夹拼接:
img_path = os.path.join(base_path,"img")
#获取响应:
response = requests.get("http://pic.yesky.com/c/6_20491_1.shtml")
#将请求结果交给bs4解析:
soup = BeautifulSoup(response.text,"html.parser")
#经过分析定位到<div class="lb_box">:
div_obj = soup.find(name="div",attrs={"class":"lb_box"})
#从div中找所有的dl标签(每一张图片的外部标点)、结果是列表
list_dl = div_obj.find_all(name = "dl")
#循环每一张图片的dl:
for dl in list_dl:
#从dl中找到img:
img = dl.find(name = "img")
#从标签中获取图片的src属性:
img_src = img.get("src")
#拿到img的URL、使用requests发请求:
img_response = requests.get(img_src)
#处理路径和文件名:
file_path = os.path.join(img_path,img_src.rsplit("/",1)[-1])
with open(file_path,"wb") as f:
f.write(img_response.content)
效果如下:
![](https://img2018.cnblogs.com/i-beta/1452370/202001/1452370-20200117214422004-1892390219.png)