self.url = urlself.get_ths_bz()self.get_zb_content_list() t_html = etree.HTML(requests.get(self.url).text).xpath(‘/html/body/div[1]/a‘) tlist =[]for t in t_html: d ={} d["id"]= str(t.xpath(‘@data-statid‘)[0])[str(t.xpath(‘@data-statid‘)[0]).rindex(‘_‘)+1:] d["title"]= str(t.xpath(‘div[1]/strong/text()‘)[0].decode(‘utf-8‘).encode(‘GB18030‘)).strip().strip(‘\n‘).strip(‘\r‘) d["url"]= str(t.xpath(‘@data-clienturl‘)[0])[str(t.xpath(‘@data-clienturl‘)[0]).rindex(‘=‘)+1:] d["isAgree"]=int(t.xpath(‘div[3]/p/text()‘)[0]) d["oper"]=self.get_zb_stock_url(d["url"]) tlist.append(d)self.ths_bz_list = sorted(tlist, key=lambda s: s["isAgree"], reverse=True)print(self.ths_bz_list[:10])for bz inself.ths_bz_list[:10]: t_html = etree.HTML(requests.get(bz["url"]).text).xpath(‘//*[@id="J_Mlist"]/div‘)if len(t_html)>0: t = t_html[0] zbtime = t.xpath("p/span/text()")[0] zbtext = str(t.xpath("p/text()")[0].decode(‘utf-8‘).encode(‘GB18030‘)).strip().strip(‘\n‘).strip(‘\r‘)print(" %s %s:%s"%(bz[‘oper‘],zbtime, zbtext))self.zb_contest_list.append({"gpurl": bz[‘oper‘],"title": bz[‘title‘],"zbtime": zbtime,"zbtext": zbtext})returnself.zb_contest_list html = requests.get(bz_url).text t_html_gp = etree.HTML(html).xpath(‘//*[@id="gotracelink"]/@data-iosurl‘)if len(t_html_gp)>0:return t_html_gp[0] stock_list =[] stock_list.append({"code": u"股票编码".decode(‘utf-8‘).encode(‘GB18030‘),"name": u"股票名称".decode(‘utf-8‘).encode(‘GB18030‘),"date": u"买入日期".decode(‘utf-8‘).encode(‘GB18030‘),"money": u"盈亏金额".decode(‘utf-8‘).encode(‘GB18030‘),"rate": u"盈利率".decode(‘utf-8‘).encode(‘GB18030‘)}) t_html = etree.HTML(requests.get(gp_url).text).xpath(‘//*[@id="infoTpl"]/ul‘)if len(t_html)>0:for t in t_html:try: code = t.xpath(‘li[1]/div[2]/text()‘)[0] name = t.xpath(‘li[1]/div[1]/text()‘)[0].decode(‘utf-8‘).encode(‘GB18030‘) date = str(t.xpath(‘li[2]/text()‘)[0]).strip() money = str(t.xpath(‘li[3]/text()‘)[0]).strip() rate = str(t.xpath(‘li[4]/text()‘)[0]).strip() stock_list.append({"code": code,"name": name,"date": date,"money": money,"rate": rate})except:passreturn stock_list mymail = sendmail([‘‘]) s ="" tt =Template(mymail.title_template) tt_gp =Template(mymail.table_template)for zb inself.zb_contest_list: gp_s ="" pglist =self.get_zb_stock(zb["gpurl"])for gp in pglist:try:iffloat(gp["money"])>0.0: gp["isBold"]=‘style="color: #F00,; font-weight: bold;"‘ gp_s = gp_s + tt_gp.substitute(gp)except:pass s = s + str(tt.substitute(zb))+ gp_s +" </table>"if mymail.send_mail(u‘同花顺直播 %s ‘% time.strftime("%Y-%m-%d %H:%M", time.localtime())," %s"%(s)):print("send_mail ok!^_^")else:print("send_mail fail!~_~")<divid="J_Mlist"><strongstyle="color:red;">$title $zbtime </strong><div><p> $zbtext</p></div></div><tablewidth="400"border="1"> ‘‘‘ table_template = ‘‘‘<tr ${isBold}><td>${code}</td><td>${name}</td><td>${date}</td><td><spanclass="ping">${money}</span></td><td>${rate}</td></tr>self.mailto_list = mailto_list me =sub+"<"+self.mail_user +"@"+self.mail_postfix +">" msg =MIMEText(content, _subtype="html", _charset="gb2312") msg["Subject"]=sub msg["From"]= me msg["To"]=";".join(self.mailto_list)try: s = smtplib.SMTP() s.connect(self.mail_host) s.login(self.mail_user,self.mail_pass) s.sendmail(me,self.mailto_list, msg.as_string()) s.close()returnTrueexceptException, e:print str(e)returnFalse
原文:http://www.cnblogs.com/yinsolence/p/56a159d5eb0ea1f18b58c28bc41f6073.html