#!/usr/bin/env python#-*-coding:utf-8-*-# @Time: 2018/7/3 20:25# @Author: baoshan# @Site: # @File: Iciba . py.py# @Software: Pycharm Community edition# Crawl PowerWord daily sentence put into the database store import jsonimport requestsimport pymysqlhost = ' 10.10.1.10 ' port = 3306user = ' root ' passwd = ' root ' dbme = ' information_storage ' table = ' iciba_daily_sentence ' conn = Pymysql . Connect (Host=host, User=user, passwd=passwd, DB=DBME, Port=port, charset= "UTF8") cur = conn.cursor () data = Requests.get ("http://open.iciba.com/dsapi/"). Textjdata = json.loads (data) Dateline = Jdata.get ("Dateline") caption = Jdata.get (" Caption ") content = Jdata.get (" content ") Fenxiang_img = Jdata.get (" fenxiang_img ") Note = Jdata.get (" note ") Picture = Jdata.get ("picture") Picture2 = Jdata.get ("picture2") translation = Jdata.get ("translation") TTS = Jdata.get ("TTS") sql = ' Insert into%s (Dateline,caption,content,fenxiang_img,note,picture,picture2,translation,tts) ' Values ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s") '% (table, Dateline,caption,content,fenxiang_img,note,picture,picture2,translation,tts) print (SQL) try:cur.execute (SQL) Except:print ("error") Print (Json.dumps (Jdata, Ensure_ascii=false)) Conn.commit () Conn.close ()
The database table structure is as follows
CREATE TABLE ' iciba_daily_sentence ' ( ' id ' int (one) unsigned not NULL auto_increment, ' Dateline ' char (TEN) DEFAULT NULL, ' caption ' varchar (default null), ' content ' varchar (255) default NULL, ' fenxiang_img ' varchar (255 Default NULL, ' note ' varchar (255) default NULL, ' picture ' varchar (255) default NULL, ' picture2 ' varchar ( 255) default NULL, ' translation ' varchar (255) default NULL, ' TTS ' varchar (255) default NULL, ' gmt_create ' Timestamp NULL DEFAULT current_timestamp on UPDATE current_timestamp, PRIMARY key (' id '), UNIQUE KEY ' uniq_ Dateline ' (' Dateline ')) Engine=innodb auto_increment=16 DEFAULT charset=utf8mb4;
Thank you!
Python crawl PowerWord daily sentence, stored in MySQL