from bs4 import BeautifulSoup
import requests
def get_info():
#定义头
headers = {
"User-Agent":'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'
}
#得到响应内容
response = requests.get(url = 'https://www.mzitu.com/jiepai/',headers = headers)
#用beautifulsoup将响应内容补全期中lxml是特定对象.
soup = BeautifulSoup(response.content.decode('utf-8'),'lxml')
#使用函数find_all()可直接获得符合条件的所有内容.
result = soup.find_all('img',attrs = {'class':'lazy'})
#使用print确认是否爬取成功.
print(result)
#使用便利将没个内容取出
for value in result:
#这边的.attrs可以获取指定属性的内容,而该内容其实就是图片的网址,这样就可以将图片的代码爬取下来了.
response = requests.get(url=value.attrs['data-original'], headers = headers)
#建造一个 文件名
path_name = './'+str(value.attrs['data-original'])[-5:-8:-1]+'png'
#最后将其保存即可
with open(path_name,'wb') as f:
f.write(response.content)
get_info()