在Python中,我们可以使用多种方法来存储爬取到的数据。以下是一些建议:
import sqlite3
# 连接到数据库(如果不存在,将创建一个新文件)
conn = sqlite3.connect('data.db')
cursor = conn.cursor()
# 创建一个名为data_table的数据表
cursor.execute('''CREATE TABLE IF NOT EXISTS data_table (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
url TEXT,
data TEXT)''')
# 插入数据
data = {'title': 'Example Title', 'url': 'https://example.com', 'data': 'Example data'}
cursor.execute("INSERT INTO data_table (title, url, data) VALUES (?, ?, ?)", (data['title'], data['url'], data['data']))
conn.commit()
# 查询数据
cursor.execute("SELECT * FROM data_table")
rows = cursor.fetchall()
for row in rows:
print(row)
# 关闭数据库连接
conn.close()
import csv
# 写入CSV文件
data = {'title': 'Example Title', 'url': 'https://example.com', 'data': 'Example data'}
with open('data.csv', 'a', newline='', encoding='utf-8') as csvfile:
fieldnames = ['title', 'url', 'data']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow(data)
# 读取CSV文件
with open('data.csv', 'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)
import json
# 写入JSON文件
data = {'title': 'Example Title', 'url': 'https://example.com', 'data': 'Example data'}
with open('data.json', 'a', encoding='utf-8') as jsonfile:
json.dump(data, jsonfile)
jsonfile.write('\n')
# 读取JSON文件
with open('data.json', 'r', encoding='utf-8') as jsonfile:
data = json.load(jsonfile)
print(data)
# 写入文本文件
data = 'Example data'
with open('data.txt', 'a', encoding='utf-8') as file:
file.write(data + '\n')
# 读取文本文件
with open('data.txt', 'r', encoding='utf-8') as file:
data = file.read()
print(data)
根据你的需求和数据类型,可以选择合适的方法来存储爬取到的数据。