快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

python数据储存mysql

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import requests
import json
import pyMySQL

conn = pymysql.connect(host='localhost',port=3306,user='root',password='root',db='toutiao',charset='utf8')
cursor = conn.cursor()

url = 'http://www.toutiao.com/api/pc/focus/'
wbdata = requests.get(url).text

data = json.loads(wbdata)
news = data['data']['pc_feed_focus']
for n in news:
    title = n['title']
    img_url = n['image_url']
    url = n['media_url']
    # print(url,title,img_url)
    cursor.execute("INSERT INTO data(title,img_url,url)VALUES('{0}','{1}','{2}');".format(title,img_url,url))
    conn.commit()

cursor.close()
conn.close()
print 'ok'

文章题目:python数据储存mysql
网页地址:http://6mz.cn/article/pdgesh.html

其他资讯