# -*- coding: utf-8 -*-
import scrapy
from copy import deepcopy

class SuSpider(scrapy.Spider):
    name = 'su'
    allowed_domains = ['suning.com']
    start_urls = ['http://list.suning.com/?safp=d488778a.error1.0.4786e76351']

    def parse(self, response):
        # 获取大分类列表
        bcate_list = response.xpath("//div[@class='allsortLeft']/ul/li")
        for bcate in bcate_list:
            item = {}
            # 获取大分类class的值
            class_name = bcate.xpath("./@class").extract_first()
            # 获取所有大分类的名称
            item["BCate"] = bcate.xpath("./a/span/text()").extract_first()
            # print(item["BCate"])
            # 根据大分类的class定位每个大分类下的所有小分类
            scate_list = response.xpath("//div[@class='{}']/div".format(class_name))
            for scate in scate_list:
                # 小分类的名称
                item["SCate"] = scate.xpath("./div[1]/a/@title").extract_first()
                # 获取每个小分类下的所有标签
                tag_list = scate.xpath("./div[2]/a")
                for tag in tag_list:
                    # 每个标签的链接和名称
                    item["tag"] = tag.xpath("./text()").extract_first()
                    item["tag_link"] = "http:" + tag.xpath("./@href").extract_first()
                    # 进入列表页
                    yield scrapy.Request(
                        item["tag_link"],
                        callback=self.good_list,
                        meta={"item": deepcopy(item)}
                    )

    def good_list(self, response):
        item = deepcopy(response.meta["item"])
        # 获取当前页的所有商品列表
        li_list = response.xpath("//div[@id='product-wrap']/div/ul/li")
        for li in li_list:
            # 获取商品的图片地址,名称,价格,商品详情页的链接
            item["good_img"] = "http:"+li.xpath(".//div[@class='res-img']/div/a/img/@src").extract_first()
            item["good_name"] = li.xpath(".//div[@class='res-info']/div/a/text()").extract_first()
            item["good_price"] = li.xpath(".//div[@class='res-info']/div/span/text()").extract_first()
            item["good_href"] = li.xpath(".//div[@class='res-info']/div/a/@href").extract_first()
            # 进入商品详情页
            if item["good_href"] != "javascript:void(0);":
                yield scrapy.Request(
                    "http:"+item["good_href"],
                    callback=self.good_detail,
                    meta={"item": deepcopy(item)}
                )
        # 翻页
        next_url = response.xpath("//a[@id='nextPage']/@href").extract_first()
        if next_url:
            yield scrapy.Request(
                next_url,
                callback=self.good_list,
                meta={"item": response.meta["item"]}
            )

    def good_detail(self, response):
        item = response.meta["item"]
        # 获取当前商品的属性规格:颜色、版本、
        size_list = response.xpath("//div[@id='J-TZM']/dl")
        for size in size_list:
            size_name = size.xpath("./dt/span/text()").extract_first()
            size_value = size.xpath("./dd/ul/li/@title").extract()
            item[size_name] = size_value
        print(item)

使用scrapy爬取suning的更多相关文章

  1. Scrapy爬取美女图片 (原创)

    有半个月没有更新了,最近确实有点忙.先是华为的比赛,接着实验室又有项目,然后又学习了一些新的知识,所以没有更新文章.为了表达我的歉意,我给大家来一波福利... 今天咱们说的是爬虫框架.之前我使用pyt ...

  2. 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...

  3. scrapy爬取西刺网站ip

    # scrapy爬取西刺网站ip # -*- coding: utf-8 -*- import scrapy from xici.items import XiciItem class Xicispi ...

  4. scrapy爬取豆瓣电影top250

    # -*- coding: utf-8 -*- # scrapy爬取豆瓣电影top250 import scrapy from douban.items import DoubanItem class ...

  5. scrapy爬取极客学院全部课程

    # -*- coding: utf-8 -*- # scrapy爬取极客学院全部课程 import scrapy from pyquery import PyQuery as pq from jike ...

  6. scrapy爬取全部知乎用户信息

    # -*- coding: utf-8 -*- # scrapy爬取全部知乎用户信息 # 1:是否遵守robbots_txt协议改为False # 2: 加入爬取所需的headers: user-ag ...

  7. Scrapy爬取Ajax(异步加载)网页实例——简书付费连载

    这两天学习了Scrapy爬虫框架的基本使用,练习的例子爬取的都是传统的直接加载完网页的内容,就想试试爬取用Ajax技术加载的网页. 这里以简书里的优选连载网页为例分享一下我的爬取过程. 网址为: ht ...

  8. Scrapy爬取静态页面

    Scrapy爬取静态页面 安装Scrapy框架: Scrapy是python下一个非常有用的一个爬虫框架 Pycharm下: 搜索Scrapy库添加进项目即可 终端下: #python2 sudo p ...

  9. 用scrapy爬取京东的数据

    本文目的是使用scrapy爬取京东上所有的手机数据,并将数据保存到MongoDB中. 一.项目介绍 主要目标 1.使用scrapy爬取京东上所有的手机数据 2.将爬取的数据存储到MongoDB 环境 ...

随机推荐

  1. 7天学完Java基础之7/7

    Object类的toString方法 类Object是类层次结构的根类 每个都使用Object作为超类 所有对象都实现这个类的方法 //这个是Object类的子类,实现了其所有方法 public cl ...

  2. Access 中case when then else end不支持使用switch代替

    Access 中case when then else end不支持使用switch代替 这里主要是实现一个表中多个字段,多个字段之间作比较然后取得最大值或者最小值用来处理 case when the ...

  3. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E

    Description Bear Limak prepares problems for a programming competition. Of course, it would be unpro ...

  4. April Fools Contest 2017 F

    Description You are developing a new feature for the website which sells airline tickets: being able ...

  5. Seek the Name, Seek the Fame POJ - 2752

    Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...

  6. JSP文件过大无法编译

    JSP文件过大无法编译: The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding th ...

  7. C/C++程序计时函数gettimeofday的使用

    linux 环境下 用 clock_t发现不准. 换用 //头文件 #include <sys/time.h> //使用timeval start, end;   gettimeofday ...

  8. python2和python3的区别(转)

    基本语法差异 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3只支持uni ...

  9. 列表、margin和padding的探讨、标签的分类

    一.列表 列表分为无序列表.有序列表和自定义列表 1.无序列表   <ul></ul> 1).内部必须有子标签,<li></li> 2).ul天生自带内 ...

  10. nodejs中相互引用(循环引用)的模块分析

    话不多少,直接上源码吧: modA.js: module.exports.test = 'A'; const modB = require('./05_modB'); console.log( 'mo ...