福利向---Scrapy爬虫爬取多级图片网站

1.目标站分析

目标站网址为https://52zfl.vip/zhaifuli/list_2_1.html
每页网址有若干链接,点击每个链接,是每部图片资源的详情页面,由于图片数量较多,涉及到翻页操作。
通过分析页面html代码,提取有用部分如下:

  1. 每个一级页面网址为:https://52zfl.vip/zhaifuli/list_2_X.html (X从1开始递增)
  2. 每个二级页面名称和链接所在代码块为
 		<article class="excerpt excerpt-one">
 			<header>
				<h2><a target="_blank" href="/zhaifuli/2018/0509/5141.html" title="微博女神闫盼盼12月VIP大尺度套图[60P]">微博女神闫盼盼12月VIP大尺度套图[60P]</a>
				</h2>
			</header>
		</article>

用xpath代码可分别匹配为

  • 一级页面内所有含二级页面名称和链接的代码块:response.xpath('//article[@class="excerpt excerpt-one"]')
  • 每个代码块中的名称:xpath('./header/h2/a/text()').extract()[0]
  • 每个代码块中的链接:'https://52zfl.vip'+xpath('./header/h2/a/@href').extract()[0]
  1. 每个二级页面中图片的链接:
<article class="article-content">
  	<p>
		<img alt="微博女神闫盼盼2018年3月VIP大尺度套图" src="https://www.images.zhaofulipic.com:8819/allimg/180509/150F52E1-0.jpg">
  	</p>
	<p>
		<img alt="微博女神闫盼盼2018年3月VIP大尺度套图" src="https://www.images.zhaofulipic.com:8819/allimg/180509/150F55936-1.jpg">
	</p>
	<p>
		<img alt="微博女神闫盼盼2018年3月VIP大尺度套图" src="https://www.images.zhaofulipic.com:8819/allimg/180509/150F55560-2.jpg">
	</p>
	<p>
		<img alt="微博女神闫盼盼2018年3月VIP大尺度套图" src="https://www.images.zhaofulipic.com:8819/allimg/180509/150F5E27-3.jpg">
	</p>
</article>
  1. 每个二级页面下一页的网址格式:如本页面为https://52zfl.vip/zhaifuli/2018/0509/5141.html则下一页为https://52zfl.vip/zhaifuli/2018/0509/5141_1.html

2.创建爬虫项目

切换至目标文件夹
scrapy startproject img
创建名为img的爬虫项目

3.编写爬虫文件

直接上源码,分析见注释:

# -*- coding: utf-8 -*-
import scrapy
# 导入item中结构化数据模板
from img.items import ImgItem
import time


class XhSpider(scrapy.Spider):
    # 爬虫名称,唯一
    name = "xh"
    # 允许访问的域
    allowed_domains = ["52zfl.vip"]
    # 初始URL
    start_urls = ['https://52zfl.vip/zhaifuli/list_2_1.html']
 
    def parse(self, response):
        items = [] 
        #存放当前一级页面下所有二级页面信息      
        if response.url.startswith("https://52zfl.vip/zhaifuli/list_2_"):
        # 如果图片地址以https://52zfl.vip/zhaifuli/list_2_开头,才取其名字及地址信息
            allPics = response.xpath('//article[@class="excerpt excerpt-one"]') 
            #匹配一级页面内所有含二级页面名称和链接的代码块
            for pic in allPics:
                # 分别处理每个图片,取出名称及地址
                item = ImgItem()
                name = pic.xpath('./header/h2/a/text()').extract()[0]
                addr = pic.xpath('./header/h2/a/@href').extract()[0]
                count = self.findpage(name)
                #定义一个findpage()函数,计算当前二级页面存在多少张图片
                items.append(item)
                for page_item in items:
                #取每个二级页面信息,分别使用parse_page()函数进行抓取图片地址
                    index = 1;#当前图片计数
                    while count > 0:
                        if index == 1:
                            next_url = page_item['page_url']
                        else:
                            b = page_item['page_url']
                            next_url = b[:b.find('.html')]+"_%s.html" % index
                        count -= 4
                        index += 1
                        yield scrapy.Request(url=next_url, meta={'item_1': page_item},
                                             callback=self.parse_page)

                for i in range(1, 92):
                # 取得所有一级页面网址,调用parse()函数,获取二级页面信息
                    all_pages = 'https://52zfl.vip/zhaifuli/list_2_%s.html' % i
                    yield scrapy.Request(url=all_pages, callback=self.parse)

    def parse_page(self, response):
    #获取每个二级页面内图片地址和其他信息
        items = response.meta['item_1']
        infos = response.xpath('//article[@class="article-content"]/p/img')
        print(len(infos))
        current_milli_time = lambda: int(round(time.time() * 1000))
        for info in infos:
            item = ImgItem()
            images_urls = info.xpath('@src')[0].extract()
            item['pic_url'] = images_urls
            item['name'] = items['name']
            item['pic_name'] = str(current_milli_time())
            item['list_url'] = items['list_url']
            item['page_url'] = items['page_url']
            print(item)
            yield item

    def findpage(self,name):
        co = name[name.find('[') + 1:name.find(']') - 1]
        if co.isdigit():
            return int(co)
        else:
            return self.findpage(name[name.find(']')+1:])

源代码见:https://github.com/BluFis/Scrapy-imgSite

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值