def AddTodayInfoToDb(): enddate = str(getdayofday(-1)) url = "http://jingzhi.funds.hexun.com/jz/kaifang.aspx?&subtype=4" + "&enddate=" + enddate html_src = urllib2.urlopen(url) #download html source parser = BeautifulSoup(html_src, fromEncoding="gbk") scroll_list = parser.findAll('table', attrs={'id':'scroll_list'}) #connect to the database "hexunFund.db" cxn = connect("mysql", "hexunFund") cur = cxn.cursor() #print scroll_list[0].findAll('tr')[0].findAll('td')[1].contents[0].contents allFunds = scroll_list[0].findAll('tr') fundLength = len(allFunds) for i in range(fundLength): items = allFunds[i].findAll('td') itemLength = len(items) rowData = [enddate] for j in range(itemLength): try: #print items[j].contents[0].text.ljust(13,' '), rowData.append(items[j].contents[0].text) except: #print items[j].contents[0].ljust(13,' '), rowData.append(items[j].contents[0]) del rowData[1] del rowData[6] del rowData[9] del rowData[8] insert(cur, 'mysql', tuple(rowData)) cur.close() cxn.commit() cxn.close() os.system("notify-send 'HexunFund' 'Baby Done'")
def dayDhl(): cxn = connect("mysql", "hexunFund") cur = cxn.cursor() cur.execute("SELECT DHL FROM hx002011 ORDER BY TIME DESC LIMIT 1") for rowdata in cur.fetchall(): print rowdata if float(rowdata[0]) < -0.0 or float(rowdata[0]) > 0.0: cur.execute("SELECT * FROM hx002011 ORDER BY TIME DESC LIMIT 0, 10") #beforeValid = False emailStr = "<table border='1'><th>时间</th><th>代码</th><th>基金名称</th><th>基金净值</th><th>累计净值</th><th>日涨跌</th><th>申购</th><th>赎回</th>" emailStr = emailStr.decode('utf8') #emailStr = "<table border='1'>" for rowdata in cur.fetchall(): #if float(rowdata[-3]) < -1.0 or float(rowdata[-3]) > 1.0 or beforeValid == True: #beforeValid = True emailStr += "<tr>" for s in rowdata: emailStr += "<td><font color='#FDD0EC'>%s</font></td>" % (s,) #print s emailStr += "</tr>" emailStr += "</table>" emailStr = emailStr.encode("utf8") #print emailStr emailme(emailStr) cur.close() cxn.close()