scrapy框架爬取校花网站的升级版

           **spider目录下的文件:定义DemoSpider类**
# -*- coding: utf-8 -*-
from scrapy.spiders import CrawlSpider,Rule
from scrapy.linkextractors import LinkExtractor
from img.items import ImgItem
#from bs4 import BeautifulSoup
#import urllib
#import requests
class  DemoSpider(CrawlSpider):
    name='demo'
    start_urls = ['http://www.xiaohuar.com/list-1-2.html']

    """第一个Rule是用来筛选所有的网页
       第二个是用来搜索当前页面的所有校花的子url
       allow里面的是正则表达式索引带有这个正则的url
       restrict_xpaths限定的是搜索的范围
       callback回调函数,用来处理页面
       process_links用来定义出来url的链接,其中定义的函数要传入参数links
       follow是用来定义是否跟进

    """
    rules={Rule(LinkExtractor(allow=('http://www.xiaohuar.com/list'),
                              restrict_xpaths=("//div[@class='page_num']")),
                              #callback="paser_url",
                              follow=True),
           Rule(LinkExtractor(allow='/p',restrict_xpaths="//div[@class='title']"),
                callback="paser_item",
                follow=False
                )
           }
    def paser_item(self,response):
        item=ImgItem()
        url=response.url
        print "url=%s"%url
        #检查异常
        try:
            img_url=response.xpath("//div[@class='infoleft_imgdiv']/a/img/@src").extract()[0]
            name=response.xpath("//div[@class='infodiv']/table/tbody/tr[1]/td[2]/text()").extract()
            school=response.xpath("//div[@class='infodiv']/table/tbody/tr[5]/td[2]/text()").extract()
            if 'http://www.xiaohuar.com' not in img_url:
                item['url'] = 'http://www.xiaohuar.com'+img_url
            else:
                item['url']=img_url
            item['name'] = name
            item['school'] = school
            yield item
        except Exception:
            print 'error'


        **定义items文件**
import scrapy


class ImgItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    url=scrapy.Field()
    name=scrapy.Field()
    school=scrapy.Field()

    *******定义pipelines文件***********
    import codecs
import json
import urllib
import os
class ImgPipeline(object):
    def __init__(self):
        self.file=codecs.open('items.json','w',encoding='utf-8') #以json文件的方式打开,编码为utf-8,否则会乱码
        # self.file_path=os.path.normpath("h:\\scrapy\\img\\img_picture")
        # self.count=1
    def process_item(self, item, spider):    #必须实现的函数,用于操作item
        line=json.dumps(dict(item),ensure_ascii=False)+'\n'    #将item中的每一个数据转换成json格式的并且每一个数据都要换行娴熟
        # if not os.path.exists(self.file_path):
        #     os.mkdir(self.file_path)
        #     img_name=os.path.normpath("h:\\scrapy\\img\\img_picture\\%s.jpg"%self.count)
        #     urllib.urlretrieve(item['url'],img_name)
        #     self.count+=1

        self.file.write(line)
        return item      #最后一般都要返回item,以便后续还要操作item

    def close_file(self):
        self.file.close()

 ***********seetting****************
 #在setting文件中加上下面这句话
 ITEM_PIPELINES={
        "img.pipelines.ImgPipeline":300
}













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不才陈某

欢迎关注公众号【码猿技术专栏】

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值