Scrapy-连接数据库
warning:
这篇文章距离上次修改已过276天,其中的内容可能已经有所变动。
为了使用Scrapy连接数据库,你需要使用Scrapy的item pipeline来将爬取的数据存储到数据库中。以下是一个使用SQLite数据库的例子:
首先,安装数据库驱动(如果你使用的是SQLite):
pip install pysqlite3
然后,在你的Scrapy项目中创建一个新的pipeline:
import sqlite3
class MyPipeline(object):
def __init__(self):
self.conn = sqlite3.connect('mydatabase.db')
self.cur = self.conn.cursor()
self.cur.execute('''CREATE TABLE IF NOT EXISTS items
(id INTEGER PRIMARY KEY, url TEXT, title TEXT, description TEXT)''')
self.conn.commit()
def process_item(self, item, spider):
self.cur.execute("INSERT INTO items (url, title, description) VALUES (?, ?, ?)",
(item['url'], item['title'], item['description']))
self.conn.commit()
return item
def close_spider(self, spider):
self.conn.close()
在你的settings.py
文件中启用这个pipeline:
ITEM_PIPELINES = {
'myproject.pipelines.MyPipeline': 300,
}
确保将myproject.pipelines.MyPipeline
替换为你实际的pipeline路径。
这个pipeline会在每个item通过时被调用,并将数据插入到名为items
的SQLite表中。如果你使用的是其他数据库,如MySQL或PostgreSQL,你需要安装相应的数据库驱动(例如mysqlclient
或psycopg2
),并在pipeline中修改连接和查询逻辑以适应你的数据库。
评论已关闭