import requests标签:img,url,res,pic,num,path,图片 From: https://www.cnblogs.com/txa2003/p/16952022.html
import os
from bs4 import BeautifulSoup
import time
ua={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'}
url='https://pic.netbian.com/4kyingshi/index_{}.html'
path='D:\\爬虫图片2531\\'
if not os.path.exists(path):
os.makedirs(path)
num=1
for i in range(1,5):
wangzhi=url.format(i)
res=requests.get(wangzhi,headers=ua)
res.encoding='utf-8'
soup=BeautifulSoup(res.text,'html.parser')
imgs=soup.select('ul.clearfix>li>a>img')
print(imgs)
for img in imgs:
pic_url='https://pic.netbian.com'+img['src']
#print(pic_url)
res_pic=requests.get(pic_url,headers=ua)
time.sleep(2)
with open(path+str(num)+'.jpg','wb') as f:
f.write(res_pic.content)
print('正在存储'+path+str(num)+'.jpg')
num=num+1