python爬虫之scrapy的pipeline的使用
scrapy的pipeline是一个非常重要的模块,主要作用是将return的items写入到数据库、文件等持久化模块,下面我们就简单的了解一下pipelines的用法。
案例一:
items池
class ZhihuuserItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
id = scrapy.Field()
name = scrapy.Field()
avatar_url = scrapy.Field()
headline = scrapy.Field()
description = scrapy.Field()
url = scrapy.Field()
url_token = scrapy.Field()
gender = scrapy.Field()
cover_url = scrapy.Field()
type = scrapy.Field()
badge = scrapy.Field() answer_count = scrapy.Field()
articles_count = scrapy.Field()
commercial_question = scrapy.Field()
favorite_count = scrapy.Field()
favorited_count = scrapy.Field()
follower_count = scrapy.Field()
following_columns_count = scrapy.Field()
following_count = scrapy.Field()
pins_count = scrapy.Field()
question_count = scrapy.Field()
thank_from_count = scrapy.Field()
thank_to_count = scrapy.Field()
thanked_count = scrapy.Field()
vote_from_count = scrapy.Field()
vote_to_count = scrapy.Field()
voteup_count = scrapy.Field()
following_favlists_count = scrapy.Field()
following_question_count = scrapy.Field()
following_topic_count = scrapy.Field()
marked_answers_count = scrapy.Field()
mutual_followees_count = scrapy.Field()
participated_live_count = scrapy.Field() locations = scrapy.Field()
educations = scrapy.Field()
employments = scrapy.Field()
items
写入MongoDB数据库的基本配置
#配置MongoDB数据库的连接信息
MONGO_URL = '172.16.5.239'
MONGO_PORT = 27017
MONGO_DB = 'zhihuuser' #参数等于False,就等于告诉你这个网站你想取什么就取什么,不会读取每个网站的根目录下的禁止爬取列表(例如:www.baidu.com/robots.txt)
ROBOTSTXT_OBEY = False 执行pipelines下的写入操作
ITEM_PIPELINES = {
'zhihuuser.pipelines.MongoDBPipeline': 300,
}
settings.py
pipelines.py:
1、首先我们要从settings文件中读取数据的地址、端口、数据库名称(没有会自动创建)。
2、拿到数据库的基本信息后进行连接。
3、将数据写入数据库
4、关闭数据库
注意:只有打开和关闭是只执行一次,而写入操作会根据具体的写入次数而定。
import pymongo class MongoDBPipeline(object):
"""
1、连接数据库操作
"""
def __init__(self,mongourl,mongoport,mongodb):
'''
初始化mongodb数据的url、端口号、数据库名称
:param mongourl:
:param mongoport:
:param mongodb:
'''
self.mongourl = mongourl
self.mongoport = mongoport
self.mongodb = mongodb @classmethod
def from_crawler(cls,crawler):
"""
1、读取settings里面的mongodb数据的url、port、DB。
:param crawler:
:return:
"""
return cls(
mongourl = crawler.settings.get("MONGO_URL"),
mongoport = crawler.settings.get("MONGO_PORT"),
mongodb = crawler.settings.get("MONGO_DB")
) def open_spider(self,spider):
'''
1、连接mongodb数据
:param spider:
:return:
'''
self.client = pymongo.MongoClient(self.mongourl,self.mongoport)
self.db = self.client[self.mongodb] def process_item(self,item,spider):
'''
1、将数据写入数据库
:param item:
:param spider:
:return:
'''
name = item.__class__.__name__
# self.db[name].insert(dict(item))
self.db['user'].update({'url_token':item['url_token']},{'$set':item},True)
return item def close_spider(self,spider):
'''
1、关闭数据库连接
:param spider:
:return:
'''
self.client.close()
python爬虫之scrapy的pipeline的使用的更多相关文章
- 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- 0.Python 爬虫之Scrapy入门实践指南(Scrapy基础知识)
目录 0.0.Scrapy基础 0.1.Scrapy 框架图 0.2.Scrapy主要包括了以下组件: 0.3.Scrapy简单示例如下: 0.4.Scrapy运行流程如下: 0.5.还有什么? 0. ...
- Linux 安装python爬虫框架 scrapy
Linux 安装python爬虫框架 scrapy http://scrapy.org/ Scrapy是python最好用的一个爬虫框架.要求: python2.7.x. 1. Ubuntu14.04 ...
- Python爬虫框架Scrapy实例(三)数据存储到MongoDB
Python爬虫框架Scrapy实例(三)数据存储到MongoDB任务目标:爬取豆瓣电影top250,将数据存储到MongoDB中. items.py文件复制代码# -*- coding: utf-8 ...
- 《Python3网络爬虫开发实战》PDF+源代码+《精通Python爬虫框架Scrapy》中英文PDF源代码
下载:https://pan.baidu.com/s/1oejHek3Vmu0ZYvp4w9ZLsw <Python 3网络爬虫开发实战>中文PDF+源代码 下载:https://pan. ...
- Python爬虫框架Scrapy教程(1)—入门
最近实验室的项目中有一个需求是这样的,需要爬取若干个(数目不小)网站发布的文章元数据(标题.时间.正文等).问题是这些网站都很老旧和小众,当然也不可能遵守 Microdata 这类标准.这时候所有网页 ...
- 《精通Python爬虫框架Scrapy》学习资料
<精通Python爬虫框架Scrapy>学习资料 百度网盘:https://pan.baidu.com/s/1ACOYulLLpp9J7Q7src2rVA
- Python 爬虫6——Scrapy的安装和使用
前面我们简述了使用Python自带的urllib和urllib2库完成的一下爬取网页数据的操作,但其实能完成的功能都很简单,假如要进行复制的数据匹配和高效的操作,可以引入第三方的框架,例如Scrapy ...
随机推荐
- SpringBoot-06:SpringBoot增删改查一套完整的考试案例
本此博客记录一套考试题,随后我把项目以及题目发到github上,简单的说一下springboot的开发 本此考试题用Spring+SpringMVC+MyBatis+SpringBoot+MySQL+ ...
- forall 与 for loop 案例
create table a_tab(ver number,id number);create table b_tab(ver number,id number);set timing on DECL ...
- Zookeeper系列一:Zookeeper介绍、Zookeeper安装配置、ZK Shell的使用
https://www.cnblogs.com/leeSmall/p/9563547.html 一.Zookeeper介绍 1. 介绍Zookeeper之前先来介绍一下分布式 1.1 分布式主要是下面 ...
- web基础之http
目录 1.Http协议介绍 Http工作原理 http的请求方法 http的响应响应状态码 状态码的类别 常用HTTP状态码简要介绍 用户访问网站携带的参数,以及服务端返回的参数 (http请求报文 ...
- 错误Matplotlib is building the font cache using fc-list. This may take a moment.
这上面的错误是因为你环境中没有安装GUI工具,但是你在代码中又想要显示图片,即有下面的语句: plt.imshow(np.transpose(npimg, (, , ))) plt.show() 那么 ...
- robotframework中的try exception。断言失败后,后面语句能继续执行
1.在robot中,断言有时会失败,但不想影响后面语句的执行,这时候要用到 Run Keyword And Continue On Failure
- Spring Security(四):2.1 Introduction What is Spring Security?
Spring Security provides comprehensive security services for Java EE-based enterprise software appli ...
- 小米路由器Mesh,信号有多牛?
导读 现如今随着居住面积的增加,以前可能住在一室一厅,如今二室一厅.三室一厅都有相对应的户型,有的小伙伴甚至住上了越层,这些户型对于路由器来说非常吃力的,毕竟单台路由器的覆盖范围有限.可能大多数人会在 ...
- TIME_WAIT过多的解决方法(转)
1.参考:https://blog.csdn.net/liangzhao_jay/article/details/50546898 2.参考: [Unix 网络编程]TCP状态转换图详解 - wenq ...
- Project Tungsten:让Spark将硬件性能压榨到极限(转载)
在之前的博文中,我们回顾和总结了2014年Spark在性能提升上所做的努力.本篇博文中,我们将为你介绍性能提升的下一阶段——Tungsten.在2014年,我们目睹了Spark缔造大规模排序的新世界纪 ...