NewsSpider/news_spider/news2db.py

24 lines
555 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
#!/usr/bin/python
import json
import re
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)
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()