Python 写的一段蔚来二手上架通知简易版
咻兔哔 / 技术文章
#!/usr/bin/python3
import requests
import json
import schedule
import smtplib
import time
import logging
from email.mime.text import MIMEText

# 日志配置
# 
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"    # 日志格式化输出
DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"                        # 日期格式
fp = logging.FileHandler('sendHand.log', encoding='utf-8')
fs = logging.StreamHandler()
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT, handlers=[fp, fs])    # 调用


email_host = '发送邮件服务器'
email_user = '发送邮箱账号'
email_pwd = '发送邮箱密码'
maillist ='接受邮箱列表'

cars = set()
def checkHand():
	logging.debug('run task')
	logging.basicConfig()
	if 90000 < int(time.strftime("%H%M%S")) < 220000:
		print("run task")
		carsLen = len(cars)
		url = 'https://gateway-front.nio.com/c_webredirect/810002/api/v1/otd/mer/service-aggregation/homepage/noauth/secondHand'
		reponse = requests.post(url)
		jsons = json.loads(reponse.text)
		for items in jsons['resultData']:
			if items['type'] == 'single_goods_second_hand':
				for item in items['data']:
					if 'state_text' not in item:
						car = {
								"title":item['title'],
								"originPriceCash":item['price']['originPriceCash'],
								"currentUserPriceCash": item['price']['currentUserPriceCash'],
								"img" :item['img']
							}
						cars.add(json.dumps(car))

		if len(cars) > carsLen:
			# newCars = [];
			str = ''
			for car in list(cars):
				newCar = json.loads(car)
				# newCars.append(json.loads(car))
				str += '车辆名称(title):' + newCar['title'] + '\n'
				str += '原价(originPriceCash):' + newCar['originPriceCash'] + '\n'
				str += '当前用户价(currentUserPriceCash): ' + newCar['currentUserPriceCash'] + '\n'
				str += '图片(img):' + newCar['img'] + '\n'
			me = email_user
			msg = MIMEText(str)    # 邮件内容
			msg['Subject'] = '有新车上架,请您及时查看'    # 邮件主题
			msg['From'] = me    # 发送者账号
			msg['To'] = maillist    # 接收者账号列表
			smtp = smtplib.SMTP(email_host,port=25) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
			smtp.login(email_user, email_pwd)   # 发送者的邮箱账号,密码
			smtp.sendmail(me, maillist, msg.as_string())
			# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
			smtp.quit() # 发送完毕后退出smtp
			# print ('email send success.')
			logging.debug('email send success')

if __name__ == '__main__':
	# 每分钟秒执行一次定时任务
	schedule.every().minutes.do(checkHand)
	while True:
		schedule.run_pending()
支付宝捐赠
请使用支付宝扫一扫进行捐赠
微信捐赠
请使用微信扫一扫进行赞赏
有 0 篇文章