python 基础之 scrapy 当当数据一演示

Items 程序

import scrapy

class DangdangItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    src = scrapy.Field()
    name = scrapy.Field()
    price = scrapy.Field()

spider 程序

import scrapy

class DangSpider(scrapy.Spider):
    name = "dang"
    allowed_domains = ["category.dangdang.com"]
    start_urls = ["https://category.dangdang.com/cp01.01.02.00.00.00.html"]

    def parse(self, response):
        context_list = response.xpath("//ul[@id='component_59']/li")
        # # print(context_list)
        # name = context_list.xpath("//a/@title")
        # # print(name)
        # src = context_list.xpath("//a@href")
        # # print(src)
        # price = context_list.xpath("//p[@class='price']/span[1]/text()")
        # print(price)
        book=[]
        for context in context_list:
            name = context.xpath(".//img/@alt").extract_first()
            # print(name)
            src = context.xpath(".//img/@data-original").extract_first()
            if src:
                src = src
            else:
                src = context.xpath(".//img/@src").extract_first()
            # print(src)
            price = context.xpath(".//p[@class='price']/span[1]/text()").extract_first()
            # print(price)
            print(name,price,src)
posted @ 2024-07-16 19:17  donghongchao  阅读(3)  评论(0编辑  收藏  举报