使用爬虫下载汽车之家高清大图

好久没写爬虫,今天算是复习下Scrapy,正所谓温故而知新.

这次爬取的目标网站是汽车之家的精选高清大图.

先看看运行结果吧

使用爬虫下载汽车之家高清大图_第1张图片

使用爬虫下载汽车之家高清大图_第2张图片

使用爬虫下载汽车之家高清大图_第3张图片

页面分析

下面我们来分析一下图片精选页面

使用爬虫下载汽车之家高清大图_第4张图片

这个页面中全部都是套图,我们打开某一套图进行查看.

使用爬虫下载汽车之家高清大图_第5张图片

一开始我想从这个页面获取高清大图,无奈这些高清大图在页面源码中没有?.

最后右上角发现了一个列表模式

列表模式显示的都是一些缩略图,那么怎么把缩略图变成高清大图呢?

使用爬虫下载汽车之家高清大图_第6张图片

可以将缩略图URL里面的**t_**替换掉,就会发现缩略图就已经变成了高清大图

代码编写

爬虫编写

提取精选图片页面中的套图链接

detail_urls = response.xpath("//ul[@class='content']/li/a/@href").getall()

精选图片页面中下一页的处理

next_page = response.xpath("//div[@class='pageindex']/a[last()-1]/@href").get()
        if next_page:
            yield scrapy.Request(url=response.urljoin(next_page), callback=self.parse)

从套图页面中提取列表模式的链接

list_pattern = response.xpath("//*[@id='cMode']/div/div[@class='side']/script").get()  # 提取列表模式的URL
        list_pattern = re.findall("/photolist/.*.html", list_pattern)[0]  # 匹配列表模式的url

从列表模式中下载高清大图

category = response.xpath("//div[@class='mini_left']/a[last()-1]/text()").get()
image_urls = response.xpath("//ul[@id='imgList']/li/a/img/@src").getall()
        image_urls = list(map(lambda x: x.replace("t_", ""), image_urls))  # 去除url中的"t_"得到高清大图
        image_urls = list(map(lambda x: response.urljoin(x), image_urls))
        yield CarhomehdItem(category=category, image_urls=image_urls)

编写ItemPipeline保存图片

class ImagePipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        request_objs = super(ImagePipeline, self).get_media_requests(item, info)
        for request_obj in request_objs:
            request_obj.item = item
        return request_objs

    def file_path(self, request, response=None, info=None):
        path = super(ImagePipeline, self).file_path(request, response, info)
        category = request.item.get("category")
        image_store = IMAGES_STORE
        category_path = os.path.join(image_store, category)
        if not os.path.exists(category_path):
            os.mkdir(category_path)
        image_name = path.replace("full/", "")
        image_path = os.path.join(category_path, image_name)
        return image_path

完整代码:

https://github.com/liu726301387/CarHomeHD

你可能感兴趣的:(爬虫)