1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| import smtplib import time from email.mime.text import MIMEText import requests from lxml import etree import datetime from fake_useragent import UserAgent
sender_maile = '' sender_pass = '' boy_name = '' girl_name = '' maile_obj = smtplib.SMTP_SSL('smtp.qq.com', 465) receiver_mail = '' special_day = '' province = '' city = '' title = 's' ua = UserAgent() header = { 'Referer': 'https://tianqi.moji.com/weather/china/guangxi', 'User-Agent': ua.random }
session = requests.session()
def get_day(): d1 = datetime.datetime.strptime(special_day, '%Y-%m-%d') d2 = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d'), '%Y-%m-%d') delta = d2 - d1 return delta.days
def get_chp(): url = "https://api.lovelive.tools/api/SweetNothings" resp = requests.get(url=url) return resp.text
def get_weathertip(): try: url = f'https://tianqi.moji.com/weather/china/{province}/{city}' resp = session.get(url=url, headers=header, verify=False) html = etree.HTML(resp.text) em = html.xpath('/html/body/div[4]/div[1]/div[4]/em/text()')[0] return em except: return False
def get_weather(): try: url = f'https://tianqi.moji.com/weather/china/{province}/{city}' resp = session.get(url=url, headers=header, verify=False) html = '' htmls = etree.HTML(resp.text) ul = htmls.xpath('/html/body/div[5]/div[1]/div[1]/ul') for lis in ul: day = lis.xpath('./li[1]/a/text()')[0] src = lis.xpath('./li[2]/span/img/@src')[0] weather = lis.xpath('./li[2]/span/img/@alt')[0] temperature = lis.xpath('./li[3]/text()')[0] air = lis.xpath('./li[5]/strong/text()')[0].strip() color = str(lis.xpath('./li[5]/strong/@class')[0]) if color == 'level_1': color = '#8fc31f' elif color == 'level_2': color = '#d7af0e' elif color == 'level_3': color = '#f39800' elif color == 'level_4': color = '#e2361a' elif color == 'level_5': color = '#5f52a0' elif color == 'level_6': color = '#631541' html += """<div style="display: flex;margin-top:5px;height: 30px;line-height: 30px;justify-content: space-around;align-items: center;"> <span style="width:15%%; text-align:center;">%s</span> <div style="width:10%%; text-align:center;"> <img style="height:26px;vertical-align:middle;" src='%s' alt=""> </div> <span style="width:25%%; text-align:center;">%s</span> <div style="width:35%%; "> <span style="display:inline-block;padding:0 8px;line-height:25px;color:%s; border-radius:15px; text-align:center;">%s</span> </div> </div> """ % (day, src, temperature, color, air) return html except: return False
def get_image(): url = "http://wufazhuce.com/" resp = requests.get(url=url) html = etree.HTML(resp.text) img_url = html.xpath('//*[@id="carousel-one"]/div/div[1]/a/img/@src')[0] return img_url
def get_today(): i = datetime.datetime.now() date = "%s/%s/%s" % (i.year, i.month, i.day) return date
mail_content = """<!DOCTYPE html> <html>
<head> <title> </title> <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> <meta charset="UTF-8"> </head>
<body style="margin:0;padding:0;"> <div style="width:100%; margin: 40px auto;font-size:20px; color:#5f5e5e;text-align:center"> <span>今天是我们在一起的第</span> <span style="font-size:24px;color:rgb(221, 73, 73)" >{0}</span> <span>天</span> </div> <div style="width:100%; margin: 0 auto;color:#5f5e5e;text-align:center"> <span style="display:block;color:#676767;font-size:20px">{2}</span> <br> <span style="display:block;color:#676767;font-size:20px">{1}</span> <span style="display:block;margin-top:15px;color:#676767;font-size:15px">近期天气预报</span> {3} </div> <div style="text-align:center;margin:35px 0;"> <span style="display:block;margin-top:55px;color:#676767;font-size:15px">{4} ❤️ {5}</span> <span style="display:block;margin-top:25px;font-size:22px; color:#9d9d9d; ">{6}</span> <img src='{7}' style="width:100%;margin-top:10px;" alt=""> </div> </body>
</html>""".format(str(get_day()), get_weathertip(), get_chp(), get_weather(), boy_name, girl_name, get_today(), get_image())
def send_mail(): try: maile_obj.login(sender_maile, sender_pass) msg = MIMEText(mail_content, _subtype='html', _charset='utf-8') msg['Subject'] = title msg['From'] = "发送人名称" msg['To'] = "接收人名称" maile_obj.sendmail(sender_maile, receiver_mail, msg.as_string()) maile_obj.quit() return True except smtplib.SMTPException as e: return False
if __name__ == '__main__': send_mail() print('发送成功!') theTime = datetime.datetime.now() print(theTime)
|