(python) Gets the article data for the FM website

Source: Internet
Author: User

See a very small fresh application (matchbox, one, read FM), I also want to do a literary youth, to do a literary application, but no data, only to climb, open the FM website and Fildder


Analyze the link one by one, find the link in the circle, copy it to open in the browser, isn't that what we want?


Multiple attempts can be found that the results of each return of the link is not the same, so it is enough to have this link, the following work is to get the data, and then saved to the database.

db.py

Import Mysqldbdef Connectdb ():    conn = mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ', db= ' yuefm ', port= 3306,charset= ' UTF8 ')    cur = conn.cursor (); #获得游标    return conn,cur;def insertdb (conn,cur,data):    sql = " Insert into YUEFM (body,short_id,title,is_liked,source,likes,is_opened,date_created,int_id,id) VALUES ('%s ', '%s ', '% S ', '%s ', '%s ', '%s ', '%s ', '%s ', '%s ', '%s ') '% (data[0],data[1],data[2],data[3],data[4],data[5],data[7],data[6],data[ 8],DATA[9])    try:       # Execute SQL statement        cur.execute (SQL)       # Commit to Database Execution        conn.commit ()        print data[1]+ "----- --------Success "    except:       # Rollback When an error occurs        conn.rollback ()            print ' ERROR '    # Close database connection    Conn.close ()        

main.py

#-*-Coding:utf-8-*-   import urllibimport jsonimport dbimport threadingimport timedef timer ():    t = Threading. Timer (5,starttask) #每5s执行一次任务    t.start () def startTask ():    path = "Http://yue.fm/api/articles/random"    result = Urllib.urlopen (path). Read ()    jsonval=json.loads (Result)  #获取返回的json结构      data= jsonval.values ()    conn,cur = Db.connectdb ()    db.insertdb (conn,cur,data) #插入数据    timer () if __name__ = = ' __main__ ':    Timer () while    True:        time.sleep (1)     




(python) Gets the article data for the FM website

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.