Related operations and configuration to be stored in MySQL

Source: Internet
Author: User

#导入settings文件中的配置
From Scrapy.utils.project import get_project_settings
Class Mymysqlpipeline (object):
def open_spider (self, spider):
# Connect to the database, you need to configure it in the settings.py file
Settings = Get_project_settings ()
Host = settings[' Db_host ']
Port = settings[' Db_port ']
user = Settings[' db_user ']
Password = settings[' Db_password ']
dbname = settings[' db_name ']
Dbcharset = settings[' Db_charset ']
Self.conn = Pymysql. Connect (Host=host, Port=port, User=user, Password=password, Db=dbname, Charset=dbcharset)
Self.conn = Pymysql. Connect (Host=host, Port=port, User=user, Password=password, Db=dbname, Charset=dbcharset)

def process_item (self, item, spider):
# Write to the database
sql = ' INSERT into movies (Movie_poster, Movie_name, Movie_score, Movie_type, Movie_director, Movie_screenwriter, Movie_ Actor, movie_time, movie_content) VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s") '% (
item[' Movie_poster ', item[' movie_name '], item[' Movie_score ', item[' Movie_type '], item[' movie_director ', item[' Movie_screenwriter '], item[' movie_actor '],
item[' Movie_time '], item[' movie_content '])
# Execute SQL statement
Self.cursor = Self.conn.cursor ()
Try
#创建游标
Self.cursor.execute (SQL)
#提交操作
Self.conn.commit ()
Except Exception as E:
Print (e)
Self.conn.rollback ()
Return item

def close_spider (self, spider):
Self.cursor.close ()
Self.conn.close ()
Note: It is also necessary to define the data structure in the database, which should not be created by MySQL

Configure MySQL anywhere in the settings.py

Activate using

You can also customize it in a crawler file.



Related operations and configuration to be stored in MySQL

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.