用pushplus+python監(jiān)控亞馬遜到貨動態(tài)推送微信
xbox series和ps5發(fā)售以來,國內(nèi)黃牛價格一直居高不下。雖然海外amazon上ps5補貨很少而且基本撐不過一分鐘,但是xbox series系列明顯要好搶很多。
日亞、德亞的xbox series x/s都可以直郵中國大陸,所以我們只需要借助腳本,監(jiān)控相關(guān)網(wǎng)頁的動態(tài),在補貨的第一時刻通過微信告知我們,然后迅速人工購買即可!
需求:pushplus(需要微信關(guān)注公眾號)、python3
一、pushplus相關(guān)介紹
pushplus提供了免費的微信消息推送api,具體內(nèi)容可以參考他的官網(wǎng):pushplus(推送加)微信推送消息直達 (hxtrip.com)
我們需要用到的東西有,登陸后的個人Token(用于精準推送消息),如圖:
調(diào)用該接口可使用如下代碼,token為上面提到的你個人的token,titile對應推送標題,content對應推送內(nèi)容,此代碼借鑒了官方demo
def post_push(token, title, content): url = 'http://pushplus.hxtrip.com/send' data = { "token": token, "title": title, "content": content } body = json.dumps(data).encode(encoding='utf-8') headers = {'Content-Type': 'application/json'} requests.post(url, data=body, headers=headers)
二、整體思路
不出意外的話,你在編寫代碼時,amazon應該處于無貨狀態(tài)(有貨直接就買了啊喂)!?。∥覀冊诖藭r打開amazon頁面,可以看到如下界面:
在新版Edge瀏覽器或者chrome下,按F12查看網(wǎng)頁源碼,選定中間Currently unavailable標識的區(qū)域(五顆星下面那個,最好覆蓋范圍大一點),能看到代碼如下:
有一個比較簡單的辦法,判斷amazon是否有補貨。我們可以抓取這一部分的html源碼,存進一個文件里(txt即可)。每過一定時間,重新抓取源碼,如果這些源碼變化了,那么基本上是網(wǎng)站更新了(補貨了)。不過有個小瑕疵,這種補貨也可能是亞馬遜第三方(黃牛)補貨- -
不過總歸是有了一個判斷上新的方法嘛;其實黃牛補貨很少的,德亞上好像看不到黃牛(我個人沒見過德亞上的第三方賣xsx的),日亞上基本沒有啥黃牛賣xbox
好了,接下來,我們看看如何實現(xiàn)相關(guān)功能
三、Requests+BeautifulSoup獲取相關(guān)html源碼
我們使用Requests+BeautfifulSoup來抓取<div id = 'availability_feature_div> </div>這個標簽內(nèi)部的所有html源碼
headers = { "User-Agent": "Mozilla/5.0 (Linux; Android 9; SM-A102U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.93 Mobile Safari/537.36", 'Content-Type': 'application/json' } html = requests.get(url=self.url, headers=headers) soup = BeautifulSoup(html.text, 'lxml') html.close() target = str(soup.find('div', id='availability_feature_div'))
注意如果不加headers的話,amazon會檢測到爬蟲,不會給你返回完整html代碼。第7行把requests給close掉是因為,我在監(jiān)測時開了兩個線程同時檢測日亞和德亞,如果不加這一句的話,會被amazon認為是我在攻擊網(wǎng)站,會拒絕我的網(wǎng)絡訪問
最終的target是被轉(zhuǎn)為str格式的相應html源碼,接下來只需要將其保存到文件,每隔一定時間再次爬蟲比對就行了
四、完整代碼
import json import requests from bs4 import BeautifulSoup import filecmp import time import threading class listenThread(threading.Thread): def __init__(self, url, originFile, newFile, content): threading.Thread.__init__(self) self.url = url self.originFile = originFile self.newFile = newFile self.content = content def listen(self): headers = { "User-Agent": "Mozilla/5.0 (Linux; Android 9; SM-A102U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.93 Mobile Safari/537.36", 'Content-Type': 'application/json' } html = requests.get(url=self.url, headers=headers) soup = BeautifulSoup(html.text, 'lxml') html.close() target = str(soup.find('div', id='availability_feature_div')) filetxt = open(self.originFile, 'w', encoding='utf-8') filetxt.write(target) filetxt.close() while True: target = str(soup.find('div', id='availability_feature_div')) filetxt = open(self.newFile, 'w', encoding='utf-8') filetxt.write(target) filetxt.close() if filecmp.cmp(self.originFile, self.newFile) == False: post_push('這里輸你自己的token', 'xbox update', self.content) fileAvail = open(self.originFile, 'w') fileAvail.write(target) fileAvail.close() time.sleep(30) def run(self): self.listen() def post_push(token, title, content): url = 'http://pushplus.hxtrip.com/send' data = { "token": token, "title": title, "content": content } body = json.dumps(data).encode(encoding='utf-8') headers = {'Content-Type': 'application/json'} requests.post(url, data=body, headers=headers) if __name__ == '__main__': detect_url = 'https://www.amazon.co.jp/-/en/dp/B08GGKZ34Z/ref=sr_1_2?dchild=1&keywords=xbox&qid=1611674118&sr=8-2' #url_special = 'https://www.amazon.co.jp/-/en/dp/B08GG17K5G/ref=sr_1_6?dchild=1&keywords=xbox%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BAx&qid=1611722050&sr=8-6' url_germany = 'https://www.amazon.de/Microsoft-RRT-00009-Xbox-Series-1TB/dp/B08H93ZRLL/ref=sr_1_2?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=xbox&qid=1611742161&sr=8-2' xbox = listenThread(url=detect_url,originFile='avail.txt',newFile='avail_now.txt',content='日亞') #xbox_sp = listenThread(url=detect_url,originFile='avail_sp.txt',newFile='avail_now_sp.txt') xbox_germany = listenThread(url=url_germany,originFile='avail_sp.txt',newFile='avail_now_sp.txt',content='德亞') xbox.start() #xbox_sp.start() xbox_germany.start()
本代碼開了兩個線程分別監(jiān)控日亞和德亞的xsx,detect_url是日亞鏈接,url_germany是德亞鏈接;
注意:德亞能夠直接上,日亞如果你上不去自己想辦法(不能說的東西,你懂的)
里面OriginFile和NewFile的文件名可以隨意命名,OriginFile指的是之前爬蟲的html,NewFile是新的爬蟲html,如果內(nèi)容不一樣,就會收到微信消息推送啦
這個圖只是測試用的,這個時刻日亞也沒有真的補貨哈哈哈
以上就是用pushplus+python監(jiān)控亞馬遜到貨動態(tài)推送微信的詳細內(nèi)容,更多關(guān)于pushplus+python監(jiān)控亞馬遜到貨動態(tài)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python?tkinter庫的Text記錄點擊路經(jīng)和刪除記錄詳情
這篇文章主要介紹了python?tkinter庫的Text記錄點擊路經(jīng)和刪除記錄詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06Python+Plotly繪制精美的數(shù)據(jù)分析圖
Plotly?是目前已知的Python最強繪圖庫,比Echarts還強大許多。它的繪制通過生成一個web頁面完成,并且支持調(diào)整圖像大小,動態(tài)調(diào)節(jié)參數(shù)。本文將利用Plotly繪制精美的數(shù)據(jù)分析圖,感興趣的可以了解一下2022-05-05通過 Python 和 OpenCV 實現(xiàn)目標數(shù)量監(jiān)控
這篇文章主要介紹了如何通過 Python 和 OpenCV 實現(xiàn)目標數(shù)量監(jiān)控,本文通過實例代碼圖文的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01pytorch使用horovod多gpu訓練的實現(xiàn)
這篇文章主要介紹了pytorch使用horovod多gpu訓練的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09