Bootstrap

python爬取基金_python抓取基金信息

1 #!/usr/bin/env python2 #-*- coding: utf-8 -*-3 4 importre5 importurllib6 importtime7 fromsqlalchemyimport*8 9 classFund:10 def__init__(self, code):11 self.code=code12 self.attributes={}13 def__setitem__(self, key, value):14 self.attributes[key]=value15 def__getitem__(self, key):16 returnself.attributes[key]17 18 fund_all={}19 20 fund_url='http://my.fund.163.com/stock/rankkfs.htm'21 22 u_sock=urllib.urlopen(fund_url)23 24 fund_str=u_sock.read().decode('gb2312')25 26 fund_str=fund_str.encode('utf-8')27 28 printfund_str29 30 u_sock.close()31 32 r_item_pattern=re.compile(r'

(.*?)',re.DOTALL)33 34 r_anchor_pattern=re.compile(r' (.*?)')35 36 r_normal_pattern=re.compile(r'([-0-9]*\.*\d*)')37 38 fund_list=r_item_pattern.findall(fund_str)39 40 file_name=time.strftime('%Y%m%d')+'.html'41 42 f=open( file_name,'w')43 44 db=BoundMetaData("mysql://root:clhclh@localhost/testcase?charset=utf8", echo=True)45 46 funds=Table('funds', db, autoload=True)47 48 foriteminfund_list:49 i=funds.insert()50 s=item[1]51 f.write(s)52 anchor_tuple=r_anchor_pattern.findall(s)53 fund=Fund(anchor_tuple[0])54 fund['name']=anchor_tuple[1]55 fund['company']=anchor_tuple[2]56 normal_tuple=r_normal_pattern.findall(s)57 fund['date']=normal_tuple[0]58 fund['util']=normal_tuple[1]59 fund['total']=normal_tuple[2]60 fund['rate']=normal_tuple[3]61 funds.insert().execute({'name':fund['name'],'code':fund.code,'date':fund['date'],'util':fund['util'],'total':fund['total'],'rate':fund['rate'],'company':fund['company']})62 #sql = "insert into funds(name, code, `date`, util, total, rate, company) values('%s', '%s', '%s', '%s', '%s', '%s', '%s')" \63 #% (fund['name'], fund.code, fund['date'], fund['util'], fund['total'], fund['rate'], fund['company'])64 #try:65 #print sql66 #cursor.execute(sql)67 #except Exception, e:68 g#print e69 #fund_all[fund.code] = fund70 #f.write(s)71 #conn.commit()72 #cursor.close()73 #conn.close()74 f.close()75 76 77
;