Scrapy Connecting MongoDB

Source: Internet
Author: User
Tags mongoclient mongodb

The work you need to do to connect MongoDB in Scrapy is as follows:

Parts of the 1.settings that need to be set:

# Start pipeline Component Item_pipelines = {    'QianChengWuYu.mongoDBPiplelines.MongoDBPipline': 300 ,}
# connection parameters for Mondb ' mongodb://localhost:27017 '  'scrapy_db'

(1) Where Item_pipelines is set in the Mongodbpipline class in the pipeline file Mongodbpiplelines in the Qianchengwuyu project, the priority level is 300

(2) Where Mongodb_uri is the specified login IP here is localhost, the port is 27017,mongodb_db_name is the database name in MONGODB

The Piplines files in 2.scrapy are as follows:

"""This class is used to insert data into the MONGDB database""" fromPymongoImportmongoclient fromScrapyImportItemclassMongodbpipline (object):defOpen_spider (self, spider):"""This method is used to connect to the database"""Db_uri= Spider.settings.get ('Mongodb_uri','mongodb://localhost:27017') db_name= Spider.settings.get ('Mongodb_db_name','Scrapy_default') Self.db_client= Mongoclient ('mongodb://localhost:27017') self.db=Self.db_client[db_name]defClose_spider (self, spider):"""This method is used to close the database"""self.db_client.close ()defProcess_item (self, item, spider):"""This method is used to insert data"""self.insert_db (item)returnItemdefinsert_db (self, item):ifisinstance (item, Item): Item= Dict (item)#convert a piece of data into a dictionary formatSelf.db.books.insert_one (item)#inserting data into the collection books

Scrapy Connecting MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.