Python之路__爬虫篇:新浪新闻爬取回顾(四)

数据保存

#4- 使用Pandas 整理数据
import pandas

df = pandas.DataFrame(getNewLists(commonPage))
#5- 使用Pandas 保存数据到数据库  Excel 或 Sqlite

# Excel
df.to_excel('news.xlsx')                #存
# Sqlite
import sqlite3

#新建数据库news.sqlite  创建并保存至news表
with sqlite3.connect('news.sqlite') as db:
    df.to_sql('news', con = db)

with sqlite3.connect('news.sqlite') as db:
    df3 = pandas.read_sql_query('SELECT * FROM news', con = db)
    print('查找数据库:********', df3.head(1))


猜你喜欢

转载自blog.csdn.net/idlehand/article/details/79069659