本周需要将爬虫爬下来的数据入库,因为之前已经写好PHP的接口的,可以直接通过python调用PHP接口来实现,所以把方法总结一下。
//python编码问题,因为好久用,所以很容易出现
# -*- coding: utf8 -*-
#!/usr/bin/python
import sys
reload(sys)
sys.setdefaultencoding('utf8')
//python 连接数据库
import MySQLdb
conn = MySQLdb.connect(
host = "localhost",
port = 22,
user = "root",
passwd = "root",
db = "test",
charset = "utf8")
cur = conn.cursor()
sql = 'select title,url,publish_time from mp_articles'
cur.execute(sql)
info = cur.fetchall()
cur.close()
conn.commit()
conn.close()
//python 调用RESTFul 接口
test_data = {'title':title,'srcUrl':srcUrl,'composeTime':composeTime} #参数,以此种字典形式呈现
test_data_urlencode = urllib.url