scrapy 爬课程信息
来源:互联网 发布:游戏视频制作软件 编辑:程序博客网 时间:2024/05/17 01:03
最近在研究scrapy,有一些新心得体会,在此记录下来。
在写代码期间发现一个问题,使用2个PIPE如果不把CONCURRENT_ITEMS 设为1的话,则JSON 的内容只会记录每一个网页的最后一个信息。
setting:
import osBOT_NAME = 'cat'SPIDER_MODULES = ['cat.spiders']NEWSPIDER_MODULE = 'cat.spiders'img_path = os.path.dirname(__file__)IMAGES_STORE = img_path+'/../img'CONCURRENT_ITEMS = 1ROBOTSTXT_OBEY = TrueITEM_PIPELINES = { 'cat.MyPipelines.MyPipeline': 300, 'cat.imgPiplines.ImgPipeline':1,}
CourseItem
# -*- coding: utf-8 -*-#引入文件import scrapyclass CourseItem(scrapy.Item): #课程标题 title = scrapy.Field() #课程url url = scrapy.Field() #课程标题图片 image_url = scrapy.Field() #课程描述 introduction = scrapy.Field() #学习人数 student = scrapy.Field() #课程等级 level = scrapy.Field() #图片地址 image_path = scrapy.Field()
MyPipelines
# -*- coding: utf-8 -*-#引入文件from scrapy.exceptions import DropItemimport jsonclass MyPipeline(object): def __init__(self): #打开文件 self.file = open('data.json', 'w') #该方法用于处理数据 def process_item(self, item, spider): #读取item中的数据 line = json.dumps(dict(item), ensure_ascii=False) + "\n" #写入文件 self.file.write(line.encode('utf-8')) #返回item return item #该方法在spider被开启时被调用。 def open_spider(self, spider): pass #该方法在spider被关闭时被调用。 def close_spider(self, spider): pass
imgPipelines
# -*- coding: utf-8 -*-import scrapyfrom scrapy.contrib.pipeline.images import ImagesPipelinefrom scrapy.exceptions import DropItemclass ImgPipeline(ImagesPipeline): #通过抓取的图片url获取一个Request用于下载 def get_media_requests(self, item, info): #返回Request根据图片图片url下载 yield scrapy.Request(item['image_url']) #当下载请求完成后执行该方法 def item_completed(self, results, item, info): #获取下载地址 image_path = [x['path'] for ok, x in results if ok] #判断是否成功 if not image_path: raise DropItem("Item contains no images") #将地址存入item item['image_path'] = image_path return item
spider
#encoding:utf-8import scrapy#引入容器from cat.CourseItems import CourseItemclass MySpider(scrapy.Spider): #设置name name = "cat" #设定域名 allowed_domains = ["imooc.com"] #填写爬取地址 start_urls = ["http://www.imooc.com/course/list"] #编写爬取方法 def parse(self, response): #实例一个容器保存爬取的信息 item = CourseItem() #这部分是爬取部分,使用xpath的方式选择信息,具体方法根据网页结构而定 #先获取每个课程的div for box in response.xpath('//div[@class="index-card-container course-card-container container "]'): #获取每个div中的课程路径 item['url'] = 'http://www.imooc.com' + box.xpath('.//a/@href').extract()[0] #获取div中的课程标题 item['title'] = box.xpath('.//h3/text()').extract()[0].strip() #获取div中的标题图片地址 item['image_url'] = box.xpath('.//div[@class="course-card-bk"]/img/@src').extract()[0] #获取div中的学生人数 level,student = box.xpath('.//div[@class="course-card-info"]/text()').extract() print '--------------------' print 'stu=%s'%student print 'lev=%s'%level item['student'] = student.strip() item['level'] = level.strip() #获取div中的课程简介 item['introduction'] = box.xpath('.//p/text()').extract()[0].strip() #返回信息 yield item url = response.xpath(u'//a[contains(text(),"下一页")]/@href').extract() if url: page = 'http://www.imooc.com/course/list' + url[0] yield scrapy.Request(page,callback=self.parse)
以上
0 0
- scrapy 爬课程信息
- python scrapy 爬博客信息
- scrapy爬取酒店信息
- 【scrapy】scrapy按分类爬取豆瓣电影基础信息
- Scrapy--爬取全国天气信息
- scrapy爬虫-爬取慕课网全部课程
- 基于Scrapy爬取知乎信息
- Scrapy爬取拉勾网职位信息
- scrapy爬取豆瓣读书的图书信息
- 使用scrapy爬取域名的whois信息
- scrapy爬虫实战(二)-------------爬取IT招聘信息
- 使用scrapy框架爬取豆瓣电影top250信息
- scrapy-redis分布式爬取彩票网站信息
- 使用Scrapy框架爬取腾讯招聘信息
- scrapy实战爬取电影天堂相关信息
- 爬虫scrapy抓取w3school课程列表
- 爬虫实践---Scrapy-爬取慕课网热门课程
- 网络安全-信息安全课程
- 5句经典
- SVN服务器搭建与使用二
- 线程池常用类学习笔记
- 函数调用栈理解
- pthread多线程同步大全
- scrapy 爬课程信息
- 重学java「二」
- React实战-未来是属于React的
- 字符串核函数的快速计算(一) 翻译
- mysql常用命令
- 远程连接Linux IP
- AUTOCAD二次开发-----删除一个图层里面的所有对象
- vmware centos 虚拟机与主机通信 并且能上网
- nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type