Scrapy爬虫之网站图片爬取
发布时间
阅读量:
阅读量
第2关:爬取网站实训图片并下载
任务描述
本关任务:上一关获取了图片链接,本关需要进一步操作,将这些图片下载至指定的images文件夹中(若该文件夹不存在,则需先创建),并按照提取的信息对这些图片进行命名处理。
编程要求
第一步是分析网页中的元素,并识别图片链接中的代码模式。接着,在选定的代码文件旁边单击带有三角标志的按钮(如图所示)。随后,在页面中找到并选择items.py、pipelines.py以及主爬虫imgspier.py这三个Python脚本。并在Begin-End区间内补充必要的代码。最后在运行脚本后即可成功下载所有网页图片至images目录,并确保每个文件都按照编号正确命名。

测试说明
在完成代码编写后,请您点击测评按钮。如果出现Django启动失败的情况,请重新提交测评即可解决问题。爬虫运行完成后,在本地终端中切换至指定目录:cd /images;然后使用ls命令可以查看到该文件夹中的文件和子目录(如下图所示)。

上图红框部分的内容即为爬取到的图片。
预期输出:
爬取成功
开始你的任务吧,祝你成功!
解析:
首先点击 代码文件 ,可以看到以下

点击第一个 step2/ImgProject/index.html 后输入以下代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>花</title>
</head>
<body>
<div class="box">
<div>
<a href="/static/app1/imgs/1.png" target="_blank">
<img src="/static/app1/imgs/1.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/10.png" target="_blank">
<img src="/static/app1/imgs/10.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/11.png" target="_blank">
<img src="/static/app1/imgs/11.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/12.png" target="_blank">
<img src="/static/app1/imgs/12.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/13.png" target="_blank">
<img src="/static/app1/imgs/13.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/14.png" target="_blank">
<img src="/static/app1/imgs/14.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/15.png" target="_blank">
<img src="/static/app1/imgs/15.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/16.png" target="_blank">
<img src="/static/app1/imgs/16.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/17.png" target="_blank">
<img src="/static/app1/imgs/17.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/18.png" target="_blank">
<img src="/static/app1/imgs/18.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/19.png" target="_blank">
<img src="/static/app1/imgs/19.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/2.png" target="_blank">
<img src="/static/app1/imgs/2.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/20.png" target="_blank">
<img src="/static/app1/imgs/20.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/21.png" target="_blank">
<img src="/static/app1/imgs/21.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/22.png" target="_blank">
<img src="/static/app1/imgs/22.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/23.png" target="_blank">
<img src="/static/app1/imgs/23.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/24.png" target="_blank">
<img src="/static/app1/imgs/24.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/25.png" target="_blank">
<img src="/static/app1/imgs/25.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/3.png" target="_blank">
<img src="/static/app1/imgs/3.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/4.png" target="_blank">
<img src="/static/app1/imgs/4.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/5.png" target="_blank">
<img src="/static/app1/imgs/5.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/6.png" target="_blank">
<img src="/static/app1/imgs/6.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/7.png" target="_blank">
<img src="/static/app1/imgs/7.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/8.png" target="_blank">
<img src="/static/app1/imgs/8.png" alt="未显示">
</a>
</div>
<div>
<a href="/static/app1/imgs/9.png" target="_blank">
<img src="/static/app1/imgs/9.png" alt="未显示">
</a>
</div>
</div>
</body>
</html>
打开代码文件,在项目目录中的第二行输入以下代码:
import scrapy
class ImgprojectItem(scrapy.Item):
#********** Begin **********#
img_urls = scrapy.Field() #图片的完整链接
images = scrapy.Field() #保存图片的信息
#********** End **********#
在代码文件中再次点击后,在第三行输入以下代码块:step2/ImgProject/ImgProject/pipelines.py
import os,requests
from ImgProject import settings
class ImgprojectPipeline(object):
def process_item(self, item, spider):
#********** Begin **********#
dir_path ='{}'.format(settings.IMAGES_STORE) #文件夹路径:从配置文件settings中导入定义好的路径
if not os.path.exists(dir_path):
os.makedirs(dir_path)
name = item['images']
img_url = item['img_urls']
img_path = dir_path +'/'+ name +'.jpg' #图片的最终存储路径
img = requests.get(img_url,headers=settings.DEFAULT_REQUEST_HEADERS)
with open(img_path,'wb')as file:
file.write(img.content)
#********** End **********#
完成对指定路径文件的访问并输入以下代码块:
import scrapy
from ImgProject.items import ImgprojectItem
class ImgspierSpider(scrapy.Spider):
name = 'imgspier'
allowed_domains = ['127.0.0.1']
start_urls = ['http://127.0.0.1:8080/imgs/']
def parse(self, response):
#********** Begin **********#
img_srcs = response.xpath("//div[@class='box']/div/a/img/@src").extract()
for img_src in img_srcs:
name=img_src.split('/')[-1].split('.')[0]
item = ImgprojectItem()
item['img_urls'] = "http://127.0.0.1:8080" + img_src
item['images'] = name
yield item
#********** End **********#
自测运行结果:

全部评论 (0)
还没有任何评论哟~
