先建立es的mapping,也就是建立在es中建立一个空的Index,代码如下:执行后就会在es建lagou 这个index。

 
 

from datetime import datetime

from elasticsearch_dsl import DocType, Date, Nested, Boolean, \

analyzer, InnerDoc, Completion, Keyword, Text, Integer

from elasticsearch_dsl.connections import connections

 
 

connections.create_connection(hosts=["localhost"])

 
 

 
 

class LagouType(DocType):

# url_object_id = Keyword()

url = Keyword()

title = Text(analyzer="ik_max_word")

salary = Keyword()

job_city = Keyword()

work_years = Text(analyzer="ik_max_word")

degree_need = Keyword()

job_type = Text(analyzer="ik_max_word")

publish_time = Date()

tags = Text(analyzer="ik_max_word")

job_advantage = Text(analyzer="ik_max_word")

job_desc = Text(analyzer="ik_max_word")

job_addr = Text(analyzer="ik_max_word")

company_url = Keyword()

company_name = Text(analyzer="ik_max_word")

crawl_time = Date()

 
 

# min_salary = Integer()

# max_salary = Integer()

 
 

class Meta:

index = 'lagou'

doc_type = "jobs"

 
 

 
 

if __name__ == "__main__":

LagouType.init()

 
 

接着在items 中定义到保存到es的代码,代码如下:

 
 

from
lagou.models.es_type
import
LagouType

class LagouJobItem(scrapy.Item):

 
 

url_object_id = scrapy.Field()

url = scrapy.Field()

title= scrapy.Field()

salary= scrapy.Field()

job_city= scrapy.Field()

work_years= scrapy.Field()

degree_need= scrapy.Field()

job_type= scrapy.Field()

publish_time = scrapy.Field()

tags= scrapy.Field()

job_advantage= scrapy.Field()

job_desc= scrapy.Field()

job_addr= scrapy.Field()

company_url = scrapy.Field()

company_name= scrapy.Field()

crawl_time= scrapy.Field()

min_salary=scrapy.Field()

max_salary= scrapy.Field()

 
 

def save_to_es(self):

lagou_type=LagouType()

lagou_type.url=self["url"]

lagou_type.title=self["title"]

lagou_type.salary=self["salary"]

lagou_type.job_city=self["job_city"]

lagou_type.work_years=self["work_years"]

lagou_type.degree_need=self['degree_need']

lagou_type.job_type=self['job_type']

lagou_type.publish_time=self['publish_time']

lagou_type.tags=self['tags']

lagou_type.job_advantage=self['job_advantage']

lagou_type.job_desc=self['job_desc']

lagou_type.job_addr=self['job_addr']

lagou_type.company_url=self['company_url']

lagou_type.company_name=self['company_name']

lagou_type.crawl_time=self['crawl_time']

lagou_type.meta.id=self['url_object_id']

 
 

lagou_type.save()

 
 

return

 
 

接下来就是在piplines文件中定义保存到es的pipline

 
 

class ElasticsearchPipline(object):

def process_item(self, item, spider):

item.save_to_es()

return item

 
 

之后就是到settings中进行设置。把这个pipline加入到item_pipline中

这样就可以将爬取到的数据保存到es中

 
 

详细说明:

elasticsearch官方也提供了一个python操作elasticsearch(搜索引擎)的接口包,就像sqlalchemy操作数据库一样的ORM框,这样我们操作elasticsearch就不用写命令了,用elasticsearch-dsl-py这个模块来操作,也就是用python的方式操作一个类即可

 
 

elasticsearch-dsl-py下载

下载地址:https://github.com/elastic/elasticsearch-dsl-py

文档说明:http://elasticsearch-dsl.readthedocs.io/en/latest/

首先安装好elasticsearch-dsl-py模块

 
 

、elasticsearch-dsl模块使用说明

create_connection(hosts=['127.0.0.1']):连接elasticsearch(搜索引擎)服务器方法,可以连接多台服务器

class Meta:设置索引名称和表名称

索引类名称.init(): 生成索引和表以及字段

实例化索引类.save():将数据写入elasticsearch(搜索引擎)

from elasticsearch_dsl.connections import connections # 导入连接elasticsearch(搜索引擎)服务器方法
connections.create_connection(hosts=['127.0.0.1']) #连接到本地

class lagouType(DocType): # 自定义一个类来继承DocType类
# Text类型需要分词,所以需要知道中文分词器,ik_max_wordwei为中文分词器
title = Text(analyzer="ik_max_word") # 设置,字段名称=字段类型,Text为字符串类型并且可以分词建立倒排索引
description = Text(analyzer="ik_max_word")
keywords = Text(analyzer="ik_max_word")
url = Keyword() # 设置,字段名称=字段类型,Keyword为普通字符串类型,不分词
riqi = Date() # 设置,字段名称=字段类型,Date日期类型

class Meta: # Meta是固定写法
index = "lagou" # 设置索引名称(相当于数据库名称)
doc_type = 'jobs' # 设置表名称

if __name__ == "__main__": # 判断在本代码文件执行才执行里面的方法,其他页面调用的则不执行里面的方法
lagouType.init() # 生成elasticsearch(搜索引擎)的索引,表,字段等信息

 
 

1.scrapy爬取的数据保存到es中的更多相关文章

  1. 将爬取的数据保存到mysql中

    为了把数据保存到mysql费了很多周折,早上再来折腾,终于折腾好了 安装数据库 1.pip install pymysql(根据版本来装) 2.创建数据 打开终端 键入mysql -u root -p ...

  2. 使用scrapy爬取的数据保存到CSV文件中,不使用命令

    pipelines.py文件中 import codecs import csv # 保存到CSV文件中 class CsvPipeline(object): def __init__(self): ...

  3. 顺企网 爬取16W数据保存到Mongodb

    import requests from bs4 import BeautifulSoup import pymongo from multiprocessing.dummy import Pool ...

  4. 使用logstash拉取MySQL数据存储到es中的再次操作

    使用情况说明: 已经使用logstash拉取MySQL数据存储到es中,es中也创建了相应的索引,也存储了数据.假若把这个索引给删除了,再次进行同步操作的话要咋做,从最开始的数据进行同步,而不是新增的 ...

  5. scrapy爬取海量数据并保存在MongoDB和MySQL数据库中

    前言 一般我们都会将数据爬取下来保存在临时文件或者控制台直接输出,但对于超大规模数据的快速读写,高并发场景的访问,用数据库管理无疑是不二之选.首先简单描述一下MySQL和MongoDB的区别:MySQ ...

  6. c# 抓取和解析网页,并将table数据保存到datatable中(其他格式也可以,自己去修改)

    使用HtmlAgilityPack 基础请参考这篇博客:https://www.cnblogs.com/fishyues/p/10232822.html 下面是根据抓取的页面string 来解析并保存 ...

  7. Excel文件数据保存到SQL中

    1.获取DataTable /// <summary> /// 查询Excel文件中的数据 /// </summary> /// <param name="st ...

  8. Redis使用场景一,查询出的数据保存到Redis中,下次查询的时候直接从Redis中拿到数据。不用和数据库进行交互。

    maven使用: <!--redis jar包--> <dependency> <groupId>redis.clients</groupId> < ...

  9. python爬取数据保存到Excel中

    # -*- conding:utf-8 -*- # 1.两页的内容 # 2.抓取每页title和URL # 3.根据title创建文件,发送URL请求,提取数据 import requests fro ...

随机推荐

  1. javascript的call和apply

    coffeescript里,每个文件编译成JS后,都是(function(){...}).call(this);的架势 这个call,该怎么理解呢? 在javascript里面,call 或者 app ...

  2. 怎样编辑LRC歌词

    恭喜恭喜歌词 唐嫣 - 音乐巴士 http://www.yy8844.cn/geci/mswcn/nvunns.shtml 恭喜恭喜歌词感谢 音乐巴士 珍妮 编辑歌词匹配时间为: 03 分 06 秒 ...

  3. oc83--自定义类实现copy方法

    // // main.m // 自定义类实现copy #import <Foundation/Foundation.h> #import "Person.h" #imp ...

  4. bzoj3907 网格 & bzoj2822 [AHOI2012]树屋阶梯——卡特兰数+高精度

    题目:bzoj3907:https://www.lydsy.com/JudgeOnline/problem.php?id=3907 bzoj2822:https://www.lydsy.com/Jud ...

  5. bzoj2142 礼物——扩展卢卡斯定理

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2142 前几天学了扩展卢卡斯定理,今天来磕模板! 这道题式子挺好推的(连我都自己推出来了) , ...

  6. robotframework - 运行报错提示 No keyword with name 'Open Browser' found.

    用下面的例子为例: 1.输入以上robot脚本提示: 2.经查阅资料,大部分都使用的是selenium2 版本,无法解该的问题,目前小编使用的是selenium3,不知道selenium是哪个版本的话 ...

  7. $luogu2375[NOI2014]$

    \(problem\) 其中,\(next[i],next[next[i]],next[next[next[i]]]......\)都是这个前缀串i的公共前后缀,而且只有它们是公共前后缀 那么,我们其 ...

  8. 使用Boost.Python构建混合系统(译)

    目录 Building Hybrid Systems with Boost.Python 摘要(Abstract) 介绍(Introduction) 设计目标 (Boost.Python Design ...

  9. hdu2030

    http://acm.hdu.edu.cn/showproblem.php?pid=2030 #include<stdio.h> #include<math.h> #inclu ...

  10. 研磨JavaScript系列(三):函数的魔力

    JavaScript的代码中就只有function一种形式,function就是函数的类型.在其他的编程语言中可能还存在Procedure或者是method等代码概念,在JavaScript中只有fu ...