scrapy之千图网全站爬虫

好久没写爬出了,这段时间都这折腾别的,今天看了个视频爬图片,自己无聊也写了个千图网的爬虫,结果写了好久,真是生疏,等把web知识补完一定要多写啊。

好了,我们先看看网站,看看如何遍历全站,截图是我选择的入口

《scrapy之千图网全站爬虫》 Paste_Image.png

既然找到了遍历的入口,接下来就简单了。这里讲下主要思路,跟之前爬宜搜全站一样,先爬主页的所有子栏目的网址,然后根据每个子栏目的页数构造出每一个页面的网址,之后就是遍历全站了,这里贴一下scrapy里面主要spider是代码

# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from qiantu_spider.items import QiantuSpiderItem



class QiantuSpider(scrapy.Spider):
    name = "qiantu"
    allowed_domains = ["58pic.com"]
    start_urls = ['http://58pic.com/']

    def parse(self, response):
        all_url = response.xpath('//div[@class="moren-content"]/a/@href').extract()

        #print(all_url)
        for i in range(0,int(len(all_url))):
            single_url = all_url[i]
            each_html = single_url + '0/day-1.html'  # 将每个页面构造成第一页的网址,方便提取每页的最大页数
            yield Request(each_html,callback=self.list_page,meta={'front':single_url})#把每个子网站传到下面的函数

    def list_page(self,response):
        front_url = response.meta['front']
        try:
            max_page = response.xpath('//*[@id="showpage"]/a[8]/text()').extract()[0]#提取最大页数
            print(max_page)
            #print(front_url)
            try:
                for i in range(1,int(max_page)+1):
                    img_page = front_url+'0/day-'+str(i)+'.html'#构造出每一个分类的所有url,接下来就是提取图片地址了
                    #print(img_page)
                    yield Request(url=img_page,callback=self.get_img_link)
            except:
                print('该网页没有数据')
        except Exception as  e:
            print('网页没有最大页数,作废网页')


    def get_img_link(self,response):
        item =QiantuSpiderItem()
        img_link1 = response.xpath("//a[@class='thumb-box']/img/@src").extract()
        if img_link1:
        #该网站图片有点奇葩,有些页面的图片存储方式不一样,总体来说是这两者,分开写就好了
            item['img_urls'] =img_link1
            #print(1,img_link1)
            yield item
        else:
            img_link2=response.xpath('//*[@id="main"]/div/div/div/div/div/a/img/@src').extract()
            item['img_urls'] = img_link2
            yield item
            #print(2,img_link2)

下面是piplines代码,主要是把图面下载到指定的文件夹,用了urlretrieve方法

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import urllib.request
import re
import os
class QiantuSpiderPipeline(object):
    def process_item(self, item, spider):
        for url in item['img_urls']:
            try:
                real_url = re.sub(r'!(.*)','',url)#把每个图片地址!号后面的字符去掉,剩下的是高清图地址
                name = real_url[-24:].replace('/','')#去除不能表示文件名的符号,这里将我搞死了

                #print(name)
                file ='E://qiantu/'


                urllib.request.urlretrieve(real_url,filename=file+name)

            except Exception as e:
                print(e,'该图片没有高清地址')
        print('成功下载一页图片')

千图网全站的爬取很简单,不过记得要在settings里面把robot.txt协议改掉,最好也伪造一下useragent
如图是短短几分钟的爬取效果

《scrapy之千图网全站爬虫》 Paste_Image.png

下次要挑个特别有难度的网站爬了才行,github地址:https://github.com/xiaobeibei26

    原文作者:蜗牛仔
    原文地址: https://www.jianshu.com/p/5dc4171d24d9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞