1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import pymysql
class MysqlPipeline: def __init__(self, host, port, user, password, db): self.conn = pymysql.connect( host=host, port=port, user=user, password=password, database=db, charset='utf8mb4' ) def process_item(self, item, spider): with self.conn.cursor() as cursor: sql = ''' INSERT INTO articles (title, url, content, created_at) VALUES (%s, %s, %s, %s) ''' cursor.execute(sql, ( item['title'], item['url'], item['content'], item['created_at'] )) self.conn.commit() return item
|