关于前面的知识点可以通过下面链接对比查看爬虫学习1--使用requests库与re库对网页进行爬取示例讲解_我就是一个小怪兽的博客-CSDN博客
图片来源于网络随意找到的一个图库,
import requests
from bs4 import BeautifulSoup
url='https://www.woyaogexing.com/shouji/'
r=requests.get(url,verify=False)
r.encoding='utf-8'
tt=r.text#获取需要的源代码
main_page=BeautifulSoup(tt,'html.parser')
imgs=main_page.find_all('a',attrs={'class':'img'})
for img in imgs:
big_link=img.find('img',attrs={'class':'lazy'}).get('src')#使用get获取标签中的属性src
img_name=big_link.split('/')[-1]
print(big_link)
print('over '+img_name)
img_resp=requests.get('http:'+big_link,verify=False,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'})
with open(img_name,mode='wb') as f:
f.write(img_resp.content)#使用content拿到图片的字节,然后write
print('over!!')
r.close()#最后关闭请求