本代码实现了每秒刷新一次,查看商品是否上架,如果上架将会播放音乐
import requests
from bs4 import BeautifulSoup
import lxml
import time
from playsound import playsound
def sleeptime(hour,min,sec):
return hour*3600 + min*60 + sec
id = "58425404603"#商品的ID
second = sleeptime(0,0,1)
while 1==1:
time.sleep(second)
response = requests.post('https://home.m.jd.com/myjd/similar/list.action?skuId='+id).text
soup = BeautifulSoup(response,'lxml')
print(soup.select('#price_'+id)[0].get_text())
if(soup.select('#price_'+id)[0].get_text()!="¥-1.00"):
print("上架啦")
playsound('music.mp3')
else:
print("下架中")
python3.6环境,需要手动修改商品ID,商品id在商品链接找到,如:
https://item.m.jd.com/product/58425404603.html
ID就是58425404603
在这个代码的同一目录放上一个mp3文件,命名为“music.mp3”,上架就会播放这段mp3