一、环境准备
下载豆瓣妹子图片需要使用python爬虫,因此,首先需要准备好python环境,使用python3.x版本,同时安装相应的第三方库,如requests、BeautifulSoup,以便实现爬虫功能。
二、实现爬虫
爬虫实现的基本步骤如下:
1、发送请求,获取豆瓣妹子图片的页面源代码,其中可以获取到图片的真实地址;
2、使用BeautifulSoup解析源代码,提取出图片的地址;
3、使用requests模块,把图片地址拼接起来,发送请求,获取图片内容;
4、使用open函数,把图片内容保存到本地。
三、实现代码
下面是实现爬虫的代码:
import requests
from bs4 import BeautifulSoup
url = 'http://www.douban.com/photos/album/163336945/'
html = requests.get(url).text
soup = BeautifulSoup(html,'lxml')
img_list = soup.find_all('img',class_='photo_wrap')
for img in img_list:
img_url = img['src']
response = requests.get(img_url).content
filename = img_url[-9:]
with open(filename,'wb') as f:
f.write(response)