import mysqldbimport urllibimport webbrowser as webimport json conn= MySQLdb.connect (host= "localhost", user= "root", passwd= "sf123456", port=3306,charset= "UTF8") Cur=conn.cursor () # Cur.execute (' create database if not exists stock_db ') conn.select_db ("Db_stock") for i in range (1,4):p rint iurl= "http://q.10jqka.com.cn/interface/stock/fl/zdf/desc/" +str (i) + " /hsb/quote "Content=urllib.urlopen (URL). Read () Open (" E:\\data\\stock\\stock_b.json "," W "). Write (content) #web. Open _new_tab ("E:\\data\\stock\\stock0617.json") f=file ("E:\\data\\stock\\stock_b.json") S=json.load (f) length=len (s[' Data ']) For i in range (0,length):sql = "Insert into stock_information_b (Cje, CJL,HSL,JK,JLR,RTIME,STOCKCODE,STOCKID,STOCKNAME,ZDE,ZDF,ZDJ,ZGJ,ZS,ZXJ) VALUES ('%s ', '%s ', '%s ', '%s ', '%s ', '%s ', ' %s ', '%s ', '%s ', '%s ', '%s ', '%s ', '%s ', '%s ', '%s ') '% (s[' data '][i][' cje '],s[' data '][i][' CJL '],s[' data '][i][' HSL '],s[' Data '][i][' JK '],s[' data '][i][' JLR '],s[' data '][i][' rtime '],s[' data '][i][' stockcode '],s[' data '][i][' stockid '],s[' data '][i][' stockname '], s[' data '][i][' Zde '],s[' data '][i][' ZDF '],s[' data '][i][' zdj '],s[' "Data '][i][' zgj '"],s[' data '][i][' ' zs ' and '],s[' data '][i [' ZXJ ']) cur.execute (SQL) Conn.commit () print "Hello" # except:# print e# conn.rollback ( ) Conn.close () import urllibimport webbrowser as weburl= "http://q.10jqka.com.cn/hk/hzcf/" Content=urllib.urlopen (URL). Read () Open ("E:\\data\\stock\\hengsengindex0623.html", "W"). Write (content) Web.open_ New_tab ("e:\\data\\stock\\hengsengindex0623.html")
Web crawler 2