aspen plus python_使用pushplus+python实现亚马逊到货消息推送微信

xbox series和ps5发售以来,国内黄牛价格一直居高不下。虽然海外amazon上ps5补货很少而且基本撑不过一分钟,但是xbox series系列明显要好抢很多。

日亚、德亚的xbox series x/s都可以直邮中国大陆,所以我们只需要借助脚本,监控相关网页的动态,在补货的第一时刻通过微信告知我们,然后迅速人工购买即可!

需求:pushplus(需要微信关注公众号)、python3

一、pushplus相关介绍

pushplus提供了免费的微信消息推送api,具体内容可以参考他的官网:pushplus(推送加)微信推送消息直达 (hxtrip.com)

我们需要用到的东西有,登陆后的个人Token(用于精准推送消息),如图:

62712f0f5e18c377ab1285ebe13e893e.png

调用该接口可使用如下代码,token为上面提到的你个人的token,titile对应推送标题,content对应推送内容,此代码借鉴了官方demo

defpost_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页面,可以看到如下界面:

68219f510d59cd6e999d108af82684cb.png

在新版Edge浏览器或者chrome下,按F12查看网页源码,选定中间Currently unavailable标识的区域(五颗星下面那个,最好覆盖范围大一点),能看到代码如下:

6d4840ac09fbe474108e182060c93f20.png

有一个比较简单的办法,判断amazon是否有补货。我们可以抓取这一部分的html源码,存进一个文件里(txt即可)。每过一定时间,重新抓取源码,如果这些源码变化了,那么基本上是网站更新了(补货了)。不过有个小瑕疵,这种补货也可能是亚马逊第三方(黄牛)补货-  -

不过总归是有了一个判断上新的方法嘛;其实黄牛补货很少的,德亚上好像看不到黄牛(我个人没见过德亚上的第三方卖xsx的),日亚上基本没有啥黄牛卖xbox

好了,接下来,我们看看如何实现相关功能

三、Requests+BeautifulSoup获取相关html源码

我们使用Requests+BeautfifulSoup来抓取

4 }5 html = requests.get(url=self.url, headers=headers)6 soup = BeautifulSoup(html.text, 'lxml')7 html.close()8 target = str(soup.find('div', id='availability_feature_div'))

注意如果不加headers的话,amazon会检测到爬虫,不会给你返回完整html代码。第7行把requests给close掉是因为,我在监测时开了两个线程同时检测日亚和德亚,如果不加这一句的话,会被amazon认为是我在攻击网站,会拒绝我的网络访问

最终的target是被转为str格式的相应html源码,接下来只需要将其保存到文件,每隔一定时间再次爬虫比对就行了

四、完整代码

1 importjson2 importrequests3 from bs4 importBeautifulSoup4 importfilecmp5 importtime6 importthreading7

8

9 classlistenThread(threading.Thread):10 def __init__(self, url, originFile, newFile, content):11 threading.Thread.__init__(self)12 self.url =url13 self.originFile =originFile14 self.newFile =newFile15 self.content =content16

17 deflisten(self):18 headers ={19 "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",20 'Content-Type': 'application/json'

21 }22 html = requests.get(url=self.url, headers=headers)23 soup = BeautifulSoup(html.text, 'lxml')24 html.close()25 target = str(soup.find('div', id='availability_feature_div'))26 filetxt = open(self.originFile, 'w', encoding='utf-8')27 filetxt.write(target)28 filetxt.close()29 whileTrue:30 target = str(soup.find('div', id='availability_feature_div'))31 filetxt = open(self.newFile, 'w', encoding='utf-8')32 filetxt.write(target)33 filetxt.close()34 if filecmp.cmp(self.originFile, self.newFile) ==False:35 post_push('这里输你自己的token', 'xbox update', self.content)36 fileAvail = open(self.originFile, 'w')37 fileAvail.write(target)38 fileAvail.close()39 time.sleep(30)40 defrun(self):41 self.listen()42

43

44 defpost_push(token, title, content):45 url = 'http://pushplus.hxtrip.com/send'

46 data ={47 "token": token,48 "title": title,49 "content": content50 }51 body = json.dumps(data).encode(encoding='utf-8')52 headers = {'Content-Type': 'application/json'}53 requests.post(url, data=body, headers=headers)54

55

56 if __name__ == '__main__':57 detect_url = 'https://www.amazon.co.jp/-/en/dp/B08GGKZ34Z/ref=sr_1_2?dchild=1&keywords=xbox&qid=1611674118&sr=8-2'

58 #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'

59 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'

60 xbox = listenThread(url=detect_url,originFile='avail.txt',newFile='avail_now.txt',content='日亚')61 #xbox_sp = listenThread(url=detect_url,originFile='avail_sp.txt',newFile='avail_now_sp.txt')

62 xbox_germany = listenThread(url=url_germany,originFile='avail_sp.txt',newFile='avail_now_sp.txt',content='德亚')63 xbox.start()64 #xbox_sp.start()

65 xbox_germany.start()

本代码开了两个线程分别监控日亚和德亚的xsx,detect_url是日亚链接,url_germany是德亚链接;

注意:德亚能够直接上,日亚如果你上不去自己想办法(不能说的东西,你懂的)

里面OriginFile和NewFile的文件名可以随意命名,OriginFile指的是之前爬虫的html,NewFile是新的爬虫html,如果内容不一样,就会收到微信消息推送啦

4f7463cb125b2b7b388cf50b0e3a1bd2.png      这个图只是测试用的,这个时刻日亚也没有真的补货哈哈哈

现在是2021.1.27 XSX和PS5都还处于需要抢的阶段,祝大家尽早进入次世代哦

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值