Scrapy爬一下苏宁图书

别说了,GKD,滴滴学生卡

不上注释了哦 如果那里我写的不明白就留言哈

# -*- coding: utf-8 -*-
import scrapy,re
from copy import deepcopy
# from book.items import BookItem

class SuningSpider(scrapy.Spider):
    name = 'suning'
    allowed_domains = ['suning.com']
    start_urls = ['https://book.suning.com']

    def parse(self, response):
        dl_list = response.xpath('//div[@class="menu-list"]//dl')
        for dl in dl_list:
            item = {}
            item["menu_list"] = dl.xpath('./dt/h3/a/text()').extract_first()
            for dd in dl_list:
                item["small_list"] = dd.xpath('./dd/a/text()').extract_first()
                item["small_href"] = dd.xpath('./dd/a/@href').extract_first()
                yield scrapy.Request(
                    item["small_href"],
                    callback=self.parse_smallhref,
                    meta={"item":deepcopy(item)}
                )

    def parse_smallhref(self,response):
        item = response.meta["item"]
        li_list = response.xpath('//div[@id="filter-results"]/ul')
        for li in li_list:
            item["book_name"] = li.xpath('.//div[@class="res-info"]/p[2]/a/text()').extract_first()
            item["book_href"] = 'https:' + li.xpath('.//div[@class="res-info"]/p[2]/a/@href').extract_first()
            yield scrapy.Request(
                item["book_href"],
                callback=self.parse_detail,
                meta={"item":deepcopy(item)}
            )

        currentpage = int(re.findall('param.currentPage = "(.*?)"',response.text)[0])
        pagenum = int(re.findall('param.pageNumbers = "(.*?)"',response.text)[0])
        nextpage = currentpage+1
        categoryId = re.findall('"categoryId": "(.*?)"',response.text)[0]
        if currentpage < pagenum:
            next_url = 'https://list.suning.com/1-{}-{}.html'.format(categoryId,str(nextpage))
            yield scrapy.Request(
                next_url,
                callback=self.parse_smallhref,
                meta={"item":deepcopy(item)}
            )

    def parse_detail(self,response):
        item = response.meta["item"]
        item["book_price"] = re.findall('"itemPrice":"(.*?)"',response.text)[0]
        item["book_author"] = response.xpath('//ul[@class="bookcon-param clearfix"]/li[1]/span/text()').extract_first()
        item["book_public"] = response.xpath('//ul[@class="bookcon-param clearfix"]/li[2]/text()').extract_first()
        yield deepcopy(item)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值