当前位置 博文首页 > LY的博客:使用python爬取亚马逊商品switch到货通知、价格,并发
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import pandas as pd
import requests
from bs4 import BeautifulSoup
flag_ex=0
flag=0
#邮件核心
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def fasong(message,targetmail):
'''发送邮箱'''
sender = '***@163.com' #企业263邮箱
'''接收邮箱'''
receiver = targetmail
'''发送邮件主题'''
subject = '阿阳消息关于switch有货'
'''发送邮箱服务器'''
smtpserver = 'smtp.163.com'
'''发送邮箱用户/密码'''
username = '***@163.com'
password = '***'
'''中文需参数‘utf-8’ ,单字节字符不需要'''
msg = MIMEText(message,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = '***<***@163.com>'
msg['To'] = targetmail
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print ("Email has been sent out!")
# In[12]:
#主市场
def am():
htm=requests.get('http://www.nowinstock.net/videogaming/consoles/nintendoswitch/#')
htm.encoding='utf-8'
soup = BeautifulSoup(htm.text)
thing=soup.select('#tr45408 > td.stockStatus')[0]
thing.text
return thing.text
# In[13]:
#日版
def jp():
url='http://www.nowinstock.net/jp/videogaming/consoles/nintendoswitch/'
htm=requests.get(url)
htm.text
soup = BeautifulSoup(htm.text)
thing=soup.select('#tr46705 > td.stockStatus')[0]
thing.text
return thing.text
#tr45419 > td.stockStatus
# In[25]:
#UK
def uk():
url='http://www.nowinstock.net/uk/videogaming/consoles/nintendoswitch/'
htm=requests.get(url)
htm.text
soup = BeautifulSoup(htm.text)
thing=soup.select('#tr45419 > td.stockStatus')[0]
thing.text
return thing.text
# In[23]:
url='http://www.nowinstock.net/uk/videogaming/consoles/nintendoswitch/'
htm=requests.get(url)
htm.text
soup = BeautifulSoup(htm.text)
thing=soup.select('#tr45419 > td.stockStatus')[0]
thing.text
#tr45419 > td.stockStatus
#tr45419 > td.stockStatus
# In[ ]:
import time
while True:
try:
text0=am()
text1=jp()
text2=uk()
textlist=[text0,text1,text2]
for a in textlist:
if a=='Preorder' or a == 'In Stock':
flag_ex=flag
flag=1
print('yes')
fasong(a,'***@163.com')
break
else:
flag=0
print('no')
except:
print('fail to do this')
pass
time.sleep(300)
我是半年前挂在VPS运行的,然后借助这个通知帮朋友购置了英版的switch森友版,比淘宝海淘便宜多了。现在vps到期了,我就把这段代码发出来,作为一段纪念和回忆了,希望能帮到有想法的朋友。
cs