先记录代码吧,回头再写文字:
# -*- coding: utf-8 -*-
import scrapy
import json
import pprint
from copy import deepcopy
class JdSpider(scrapy.Spider):
name = 'jd'
allowed_domains = ['jd.com', 'p.3.cn']
# 这是由于后期使用有使用json解析,使用的域名为p.3.cn
start_urls = ['https://book.jd.com/booksort.html']
def parse(self, response):
dt_list = response.xpath('//div[@class="mc"]/dl/dt') # 获取大分类列表
for dt in dt_list:
item = dict() # 创建字典,存储相关信息
item['b_cate'] = dt.xpath('./a/text()').extract_first() # 大分类标题
em_list = dt.xpath('./following-sibling::dd[1]/em') # 获取小分类列表位置
for em in em_list: # 小分类列表
item["s_href"] = em.xpath('./a/@href').extract_first()
item['s_cate'] = em.xpath('./a/text()').extract_first