NewsSpider/news_spider/news2db.py

24 lines
555 B
Python
Raw Normal View History

2016-04-24 00:17:26 +08:00
# -*- coding: utf-8 -*-
#!/usr/bin/python
import json
import re
2016-04-24 00:17:26 +08:00
import sqlite3
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
file = open('title.json')
conn = sqlite3.connect('news.db')
while 1:
line = file.readline()
if not line:
break
line = re.sub("'","",line)
2016-04-24 00:17:26 +08:00
data = json.loads(line)
insertsql = "insert into news(title,time,url) values ('"+str(data['title']).decode('utf-8')+"','"+str(data['time']).decode('utf-8')+"','"+str(data['url']).decode('utf-8')+"')"
print insertsql
conn.execute(insertsql)
conn.commit()
conn.close()