python简单爬虫定时推送同花顺直播及荐股至邮箱

  • 1、初衷:实践
  • 2、技术:python requests Template
  • 3、思路:根据直播页面获取评价最高的前十博主,定时爬行最新的消息和实战股票
  • 4、思路:python 编辑简单邮件html模板
  • 5、难点:邮件html模板的设计,还需要邮箱支持

    爬虫文件

    '''
    -- #coding:utf-8
    import requests
    from lxml import etree
    from sendmail import sendmail
    import sys, time
    from string import Template
    reload(sys)
    sys.setdefaultencoding('utf-8')

    1 定义类

    class thszb:
    thsbz_list = []
    zb_contest_list = []
    stock_list = []
    def __init
    (self, url):
    1. self.url = url
    2. self.get_ths_bz()
    3. self.get_zb_content_list()

    2 获取博主基本信息

    def get_ths_bz(self):
    1. t_html = etree.HTML(requests.get(self.url).text).xpath('/html/body/div[1]/a')
    2. tlist =[]
    3. for t in t_html:
    4. d ={}
    5. d["id"]= str(t.xpath('@data-statid')[0])[str(t.xpath('@data-statid')[0]).rindex('_')+1:]
    6. d["title"]= str(t.xpath('div[1]/strong/text()')[0].decode('utf-8').encode('GB18030')).strip().strip(
    7. '\n').strip(
    8. '\r')
    9. d["url"]= str(t.xpath('@data-clienturl')[0])[str(t.xpath('@data-clienturl')[0]).rindex('=')+1:]
    10. d["isAgree"]=int(t.xpath('div[3]/p/text()')[0])
    11. d["oper"]=self.get_zb_stock_url(d["url"])
    12. tlist.append(d)
    13. self.ths_bz_list = sorted(tlist, key=lambda s: s["isAgree"], reverse=True)
    def get_zb_content_list(self):
    1. print(self.ths_bz_list[:10])
    2. for bz inself.ths_bz_list[:10]:
    3. t_html = etree.HTML(requests.get(bz["url"]).text).xpath('//*[@id="J_Mlist"]/div')
    4. if len(t_html)>0:
    5. t = t_html[0]
    6. zbtime = t.xpath("p/span/text()")[0]
    7. zbtext = str(t.xpath("p/text()")[0].decode('utf-8').encode('GB18030')).strip().strip('\n').strip('\r')
    8. print(" %s %s:%s"%(bz['oper'],zbtime, zbtext))
    9. self.zb_contest_list.append(
    10. {"gpurl": bz['oper'],"title": bz['title'],"zbtime": zbtime,"zbtext": zbtext})
    11. returnself.zb_contest_list

    3 获取博主实战股票页面

    def get_zb_stock_url(self, bz_url):
    1. html = requests.get(bz_url).text
    2. t_html_gp = etree.HTML(html).xpath('//*[@id="gotracelink"]/@data-iosurl')
    3. if len(t_html_gp)>0:
    4. return t_html_gp[0]

    4 获取博主实战股票信息

    def get_zb_stock(self, gp_url):
    1. stock_list =[]
    2. stock_list.append(
    3. {"code": u"股票编码".decode('utf-8').encode('GB18030'),"name": u"股票名称".decode('utf-8').encode('GB18030'),
    4. "date": u"买入日期".decode('utf-8').encode('GB18030'),
    5. "money": u"盈亏金额".decode('utf-8').encode('GB18030'),
    6. "rate": u"盈利率".decode('utf-8').encode('GB18030')})
    7. t_html = etree.HTML(requests.get(gp_url).text).xpath('//*[@id="infoTpl"]/ul')
    8. if len(t_html)>0:
    9. for t in t_html:
    10. try:
    11. code = t.xpath('li[1]/div[2]/text()')[0]
    12. name = t.xpath('li[1]/div[1]/text()')[0].decode('utf-8').encode('GB18030')
    13. date = str(t.xpath('li[2]/text()')[0]).strip()
    14. money = str(t.xpath('li[3]/text()')[0]).strip()
    15. rate = str(t.xpath('li[4]/text()')[0]).strip()
    16. stock_list.append({"code": code,"name": name,"date": date,"money": money,"rate": rate})
    17. except:
    18. pass
    19. return stock_list

    5 发送邮件

    def send_mail(self):
    1. mymail = sendmail([''])
    2. s =""
    3. tt =Template(mymail.title_template)
    4. tt_gp =Template(mymail.table_template)
    5. for zb inself.zb_contest_list:
    6. gp_s =""
    7. pglist =self.get_zb_stock(zb["gpurl"])
    8. for gp in pglist:
    9. try:
    10. iffloat(gp["money"])>0.0:
    11. gp["isBold"]='style="color: #F00,; font-weight: bold;"'
    12. gp_s = gp_s + tt_gp.substitute(gp)
    13. except:
    14. pass
    15. s = s + str(tt.substitute(zb))+ gp_s +" </table>"
    16. if mymail.send_mail(u'同花顺直播 %s '% time.strftime("%Y-%m-%d %H:%M", time.localtime())," %s"%(s)):
    17. print("send_mail ok!^_^")
    18. else:
    19. print("send_mail fail!~_~")
    '''
    if name == 'main':
    ths = ths_zb("http://t.10jqka.com.cn/m/zhibo/index.html")
    ths.send_mail()
    '''

    发送邮件

    import smtplib
    from email.mime.text import MIMEText

    1 邮件类

    class sendmail:

    2 python模板

    title_template = '''
    1. <divid="J_Mlist">
    2. <strongstyle="color:red;">$title $zbtime </strong>
    3. <div>
    4. <p>
    5. $zbtext
    6. </p>
    7. </div>
    8. </div>
    9. <tablewidth="400"border="1">
    10. '''
    11. table_template = '''
    12. <tr ${isBold}>
    13. <td>${code}</td>
    14. <td>${name}</td>
    15. <td>${date}</td>
    16. <td><spanclass="ping">${money}</span></td>
    17. <td>${rate}</td>
    18. </tr>
    '''
    mailtolist = []
    mailhost = "smtp.126.com"
    mail_user = ""
    mail_pass = ""
    mail_postfix = "126.com"
    def __init
    (self, mailto_list):
    1. self.mailto_list = mailto_list

    2发送html格式邮件

    def send_mail(self, sub, content):
    1. me =sub+"<"+self.mail_user +"@"+self.mail_postfix +">"
    2. msg =MIMEText(content, _subtype="html", _charset="gb2312")
    3. msg["Subject"]=sub
    4. msg["From"]= me
    5. msg["To"]=";".join(self.mailto_list)
    6. try:
    7. s = smtplib.SMTP()
    8. s.connect(self.mail_host)
    9. s.login(self.mail_user,self.mail_pass)
    10. s.sendmail(me,self.mailto_list, msg.as_string())
    11. s.close()
    12. returnTrue
    13. exceptException, e:
    14. print str(e)
    15. returnFalse

    效果展示

posted @ 2015-09-09 17:56  yinsolence  阅读(1756)  评论(0编辑  收藏  举报