博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scrapy 爬取当当网产品分类
阅读量:4356 次
发布时间:2019-06-07

本文共 1767 字,大约阅读时间需要 5 分钟。

#spider部分 import scrapyfrom Autopjt.items import AutopjtItemfrom scrapy.http import Requestclass AutospdSpider(scrapy.Spider):    name = "autospd"    allowed_domains = ["dangdang.com"]    start_urls = ['http://category.dangdang.com/pg1-cid4007379.html']    def parse(self, response):        item = AutopjtItem()        item['name'] =response.xpath('//a[@name="itemlist-title"]/@title').extract()        item['price'] = response.xpath('//span[@class="price_n"]/text()').extract()        item['link'] = response.xpath('//a[@name="itemlist-title"]/@href').extract()        item['comnum'] = response.xpath('//a[@name="itemlist-review"]/text()').extract()        yield item        for i in range(1,101):            url = 'http://category.dangdang.com/pg'+str(i)+'-cid4007379.html'            yield Request(url,callback=self.parse)

pipeline部分

import codecsimport jsonclass AutopjtPipeline(object):    def __init__(self):        self.file = codecs.open('D:/mydata.json','wb',encoding='utf-8')    def process_item(self, item, spider):        for j in range(0,len(item['name'])):            name = item['name'][j]            price = item['price'][j]            comnum = item['comnum'][j]            link =item['link'][j]            goods = {
'name':name,'price':price,'comnum':comnum,'link':link} i = json.dumps(dict(goods),ensure_ascii=False) line = i + '\n' self.file.write(line) return item def close_spider(self,spider): self.file.close()

item部分

import scrapyclass AutopjtItem(scrapy.Item):    # define the fields for your item here like:    # name = scrapy.Field()    name = scrapy.Field()    price = scrapy.Field()    link = scrapy.Field()    comnum = scrapy.Field()

 

转载于:https://www.cnblogs.com/Erick-L/p/6835391.html

你可能感兴趣的文章
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
如何使用mysql
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
类中的静态函数和非静态函数的区别
查看>>
windows 下安装Apache
查看>>
Fedora14 mount出现错误时解决办法【亲测有效】
查看>>
使用Visual Studio 2013进行UI自动化测试
查看>>
13-集体照
查看>>
读了曾国藩家书,,心态逐渐平和起来。搞技术的如果缺乏信念的指引,生活会很乏味无聊!...
查看>>
160809308周子济第六次作业
查看>>
sublime text3最新版本注册码(build 3143)
查看>>
linux使用技巧
查看>>
必背公式及常数
查看>>
利用CSS、JavaScript及Ajax实现图片预加载的三大方法
查看>>
js时间戳转时间格式
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>