将爬取的数据插入到数据库中。假设你有一个包含书籍信息的字典列表:
books = [
{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95},
{'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99},
# 更多书籍数据...
]
insert_query = '''
INSERT INTO books (category, author, title, price)
VALUES (%s, %s, %s, %s)
'''
for book in books:
cursor.execute(insert_query, (book['category'], book['author'], book['title'], book['price']))
connection.commit() # 提交事务