添加数据库写入功能

This commit is contained in:
lzjqsdd 2016-04-24 00:17:26 +08:00
parent 0f44411114
commit db6bd39356
2 changed files with 26 additions and 0 deletions

5
ml/div.py Normal file
View File

@ -0,0 +1,5 @@
#encoding=utf-8
import jieba
seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
for l in seg_list:
print l

21
news_spider/news2db.py Normal file
View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
#!/usr/bin/python
import json
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
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()