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