cyqmimc‘blog.

google-smtp

字数统计: 269阅读时长: 1 min
2020/03/08

电商商品降价后自动发邮件通知

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
import requests
import smtplib
import time
from bs4 import BeautifulSoup

url = 'https://www.amazon.cn/dp/B07VZZS2PW/ref=sr_1_12?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99' \
'&keywords=ipad+air3&qid=1583638573&sr=8-12 '

headers = {
"User-Agent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 '
'Safari/537.36'}

#比较
def check_price():
page = requests.get(url, headers=headers)

soup = BeautifulSoup(page.content, 'html.parser')

title = soup.find(id="productTitle").get_text()

price = soup.find(id="priceblock_ourprice").get_text()
converted_price = float(price[1:4])
if converted_price > 100:
send_mail()

#自动发邮件
def send_mail():
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('@gmail.com', '')#用户名+应用密码,需要开通两步验证申请
subject = 'Price fell down'
body = 'Check the amazon link ' + url
msg = f"Subject:{subject}\n\n{body}"
server.sendmail('@gmail.com', '@163.com', msg)#发件邮箱名称,收件邮箱名称,发送内容
print('HEY EMAIL has been')
server.quit()

#1小时执行一次
#check_price()
while True:
check_price()
time.sleep(60 * 60 * 60)#一个小时执行一次
CATALOG
  1. 1. 电商商品降价后自动发邮件通知