Python crawl PowerWord daily sentence, stored in MySQL

Source: Internet
Author: User
Tags pycharm community edition

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.