用pushplus+python監控亞馬遜到貨動態推送微信
xbox series和ps5發售以來,國內黃牛價格一直居高不下。雖然海外amazon上ps5補貨很少而且基本撐不過一分鐘,但是xbox series系列明顯要好搶很多。
日亞、德亞的xbox series x/s都可以直郵中國大陸,所以我們只需要借助腳本,監控相關網頁的動態,在補貨的第一時刻通過微信告知我們,然后迅速人工購買即可!
需求:pushplus(需要微信關注公眾號)、python3
一、pushplus相關介紹pushplus提供了免費的微信消息推送api,具體內容可以參考他的官網:pushplus(推送加)微信推送消息直達 (hxtrip.com)
我們需要用到的東西有,登陸后的個人Token(用于精準推送消息),如圖:
調用該接口可使用如下代碼,token為上面提到的你個人的token,titile對應推送標題,content對應推送內容,此代碼借鑒了官方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應該處于無貨狀態(有貨直接就買了啊喂)!!!我們在此時打開amazon頁面,可以看到如下界面:
在新版Edge瀏覽器或者chrome下,按F12查看網頁源碼,選定中間Currently unavailable標識的區域(五顆星下面那個,最好覆蓋范圍大一點),能看到代碼如下:
有一個比較簡單的辦法,判斷amazon是否有補貨。我們可以抓取這一部分的html源碼,存進一個文件里(txt即可)。每過一定時間,重新抓取源碼,如果這些源碼變化了,那么基本上是網站更新了(補貨了)。不過有個小瑕疵,這種補貨也可能是亞馬遜第三方(黃牛)補貨- -
不過總歸是有了一個判斷上新的方法嘛;其實黃牛補貨很少的,德亞上好像看不到黃牛(我個人沒見過德亞上的第三方賣xsx的),日亞上基本沒有啥黃牛賣xbox
好了,接下來,我們看看如何實現相關功能
三、Requests+BeautifulSoup獲取相關html源碼我們使用Requests+BeautfifulSoup來抓取<div id = ’availability_feature_div> </div>這個標簽內部的所有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掉是因為,我在監測時開了兩個線程同時檢測日亞和德亞,如果不加這一句的話,會被amazon認為是我在攻擊網站,會拒絕我的網絡訪問
最終的target是被轉為str格式的相應html源碼,接下來只需要將其保存到文件,每隔一定時間再次爬蟲比對就行了
四、完整代碼import jsonimport requestsfrom bs4 import BeautifulSoupimport filecmpimport timeimport threadingclass 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()
本代碼開了兩個線程分別監控日亞和德亞的xsx,detect_url是日亞鏈接,url_germany是德亞鏈接;
注意:德亞能夠直接上,日亞如果你上不去自己想辦法(不能說的東西,你懂的)
里面OriginFile和NewFile的文件名可以隨意命名,OriginFile指的是之前爬蟲的html,NewFile是新的爬蟲html,如果內容不一樣,就會收到微信消息推送啦
這個圖只是測試用的,這個時刻日亞也沒有真的補貨哈哈哈
以上就是用pushplus+python監控亞馬遜到貨動態推送微信的詳細內容,更多關于pushplus+python監控亞馬遜到貨動態的資料請關注好吧啦網其它相關文章!
相關文章: