# -*-coding=utf-8-*-
__author__ = 'Rocky'
# -*-coding=utf-8-*-
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
from email import encoders, utils
import urllib.request, urllib.error, urllib.parse
import time
import re
import sys
import os

from bs4 import BeautifulSoup

from email.header import Header
import imp

class GetContent():
    def __init__(self, id):

        # 给出的第一个参
数 就是你要下载的问题的id
        # 比如 想要下载的问题链接是 https://www.zhihu.com/question/29372574
        # 那么 就输入 python zhihu.py 29372574

        id_link = "/question/" + id
        #self.getAnswer(id_link)
        self.getstorelist();
        #self.gettuangoulist();

    def save2file(self, filename, content):
        # 保存为电子书文件
        filename = filename + ".txt"
        f = open(filename, 'a', encoding="utf-8")
        f.write(content)
        f.close()
    def gettu
angoulist(self):    
        url = "http://www.shihuo.cn/tuangou"
        print(url)
        user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
        # 构造header 伪装一下
        header = {"User-Agent": user_agent}
        req = urllib.request.Request(url, headers=header)
        try:
            resp = urllib.request.urlopen(req)
            content= resp.read().decode('utf-8');
            if content is None:
                print("Empty")
                return None
        except:
            print("Time out. Retry")
            time.sleep(30)
            # try to switch with proxy ip
            resp = urllib.request.urlopen(req)
            content = resp.read()
            if content is None:
                print("Empty")
                return None 
            return None
          # 这里已经获取了 网页的代码，接下来就是提取你想要的内容。 使用beautifulSoup 来处理，很方便
        try:
            bs = BeautifulSoup(content)
  
        except:
            print("Beautifulsoup error")
            return None
        #print(content)
        title = bs.title
        # 获取的标题
        print(title)
        filename_old = title.string.strip()
        print(filename_old)
        filename = re.sub('[\/:*?"<>|]', '-', filename_old)
        # 用来保存内容的文件名，因为文件名不能有一些特殊符号，所以使用正则表达式过滤掉
        store_list = bs.find_all("li", class_="") 
        
        html_up =  """\ 
        <html> 
          <head>识货团购</head> 
          <body>
        """
        html_middle = "";
        html_bottom = """\
          </body> 
        </html> 
        """         
        k = 0
        index = 0
        for each_list in store_list:
            store_name = each_list.find("div",class_="h2") 
            if(store_name!=None):
                print (store_name.a.string);
            store_price = each_list.find("div",class_="z2") 
            
            if(store_price!=None):
                html_middle= html_middle+str(each_list);
                '''
                store_rmb = store_price.find("span",class_="z1")
                i=0
                if(store_rmb!=None): 
                    for string in store_rmb.strings:
                        self.save2file(filename,string)
                        i = i + 1;
                        if(i ==1 and float(string)<=299):
                            print (string)
                            html_middle= html_middle+str(each_list);
                #print (store_rmb);            
            #for a in each_answer.strings:
                # 循环获取每一个答案的内容，然后保存到文件中
            #self.save2file(filename, str(a))
            '''
            k += 1
            index = index + 1
        html = html_up + html_middle + html_bottom;
        
        smtp_server = 'smtp.sina.com'
        from_mail = 'he_jingtai@sina.com'
        password = 'hjtgoodno1'
        to_mail = '1024654056@qq.com'
        
        sender = '17759118897@189.cn' 
        receiver = '1024654056@qq.com' 
        subject = '图片附件html发送邮件测试'        
        
        send_kindle=MailAtt(smtp_server,from_mail,password,to_mail)
        send_kindle.sendMail(sender,receiver,subject,html)          
    def getstorelist(self):
        url = "http://www.shihuo.cn/haitao/daigou?order=date"
        print(url)
        user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
        # 构造header 伪装一下
        header = {"User-Agent": user_agent}
        req = urllib.request.Request(url, headers=header)
        try:
            resp = urllib.request.urlopen(req)
            content= resp.read().decode('utf-8');
            if content is None:
                print("Empty")
                return None
        except:
            print("Time out. Retry")
            time.sleep(30)
            # try to switch with proxy ip
            resp = urllib.request.urlopen(req)
            content = resp.read()
            if content is None:
                print("Empty")
                return None 
            return None
          # 这里已经获取了 网页的代码，接下来就是提取你想要的内容。 使用beautifulSoup 来处理，很方便
        try:
            bs = BeautifulSoup(content)
  
        except:
            print("Beautifulsoup error")
            return None
        #print(content)
        title = bs.title
        # 获取的标题
        print(title)
        filename_old = title.string.strip()
        print(filename_old)
        filename = re.sub('[\/:*?"<>|]', '-', filename_old)
        # 用来保存内容的文件名，因为文件名不能有一些特殊符号，所以使用正则表达式过滤掉
        store_list = bs.find_all("li", class_="") 
        
        html_up =  """\ 
        <html> 
          <head>测试一下</head> 
          <body>
        """
        html_middle = "";
        html_bottom = """\
          </body> 
        </html> 
        """         
        k = 0
        index = 0
        for each_list in store_list:
            store_name = each_list.find("div",class_="title") 
            #if(store_name!=None):
                #print (store_name.a.string);
            store_price = each_list.find("div",class_="price") 
            if(store_price!=None):
                store_rmb = each_list.find("span")
                i=0
                for string in store_rmb.strings:
                    self.save2file(filename,string)
                    i = i + 1;
                    if(i ==2 and float(string)<200):
                        html_middle= html_middle+str(each_list);
                #print (store_rmb);            
            #for a in each_answer.strings:
                # 循环获取每一个答案的内容，然后保存到文件中
            #self.save2file(filename, str(a))
            k += 1
            index = index + 1
        html = html_up + html_middle + html_bottom;
        
        smtp_server = 'smtp.sina.com'
        from_mail = 'he_jingtai@sina.com'
        password = 'hjtgoodno1'
        to_mail = '1024654056@qq.com'
        
        sender = '17759118897@189.cn' 
        receiver = '1024654056@qq.com' 
        subject = '图片附件html发送邮件测试'        
        
        send_kindle=MailAtt(smtp_server,from_mail,password,to_mail)
        send_kindle.sendMail(sender,receiver,subject,html)  
    
        
    def getAnswer(self, answerID):
        host = "http://www.shihuo.cn"
        url = "http://www.shihuo.cn/haitao/daigou?order=date"
        print(url)
        user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
        # 构造header 伪装一下
        header = {"User-Agent": user_agent}
        req = urllib.request.Request(url, headers=header)

        try:
            resp = urllib.request.urlopen(req)
            content= resp.read().decode('utf-8');
            if content is None:
                print("Empty")
                return None
        except:
            print("Time out. Retry")
            time.sleep(30)
            # try to switch with proxy ip
            resp = urllib.request.urlopen(req)
            content = resp.read()
            if content is None:
                print("Empty")
                return None
        # 这里已经获取了 网页的代码，接下来就是提取你想要的内容。 使用beautifulSoup 来处理，很方便
        try:
            bs = BeautifulSoup(content)

        except:
            print("Beautifulsoup error")
            return None
        #print(content)
        title = bs.title
        # 获取的标题
        print(title)

        filename_old = title.string.strip()
        print(filename_old)
        filename = r