用pushplus+python监控亚马逊到货动态推送微信


Posted in Python onJanuary 29, 2021

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

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

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

一、pushplus相关介绍

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

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

用pushplus+python监控亚马逊到货动态推送微信

调用该接口可使用如下代码,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页面,可以看到如下界面:

用pushplus+python监控亚马逊到货动态推送微信

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

用pushplus+python监控亚马逊到货动态推送微信

有一个比较简单的办法,判断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 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()

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

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

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

用pushplus+python监控亚马逊到货动态推送微信

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

以上就是用pushplus+python监控亚马逊到货动态推送微信的详细内容,更多关于pushplus+python监控亚马逊到货动态的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python利用paramiko连接远程服务器执行命令的方法
Oct 16 Python
Python爬虫实战:分析《战狼2》豆瓣影评
Mar 26 Python
python3之模块psutil系统性能信息使用
May 30 Python
Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法
Aug 23 Python
flask 框架操作MySQL数据库简单示例
Feb 02 Python
40行Python代码实现天气预报和每日鸡汤推送功能
Feb 27 Python
python列表的逆序遍历实现
Apr 20 Python
django rest framework serializers序列化实例
May 13 Python
python统计mysql数据量变化并调用接口告警的示例代码
Sep 21 Python
Python request post上传文件常见要点
Nov 20 Python
python代码实现备忘录案例讲解
Jul 26 Python
python中mongodb包操作数据库
Apr 19 Python
用python监控服务器的cpu,磁盘空间,内存,超过邮件报警
Jan 29 #Python
python热力图实现简单方法
Jan 29 #Python
Ubuntu20.04环境安装tensorflow2的方法步骤
Jan 29 #Python
python3定位并识别图片验证码实现自动登录功能
Jan 29 #Python
python中numpy数组与list相互转换实例方法
Jan 29 #Python
详解Python中的GIL(全局解释器锁)详解及解决GIL的几种方案
Jan 29 #Python
Python爬虫获取op.gg英雄联盟英雄对位胜率的源码
Jan 29 #Python
You might like
第八节 访问方式 [8]
2006/10/09 PHP
PHP获取163、gmail、126等邮箱联系人地址【已测试2009.10.10】
2009/10/11 PHP
php 获取本机外网/公网IP的代码
2010/05/09 PHP
php中修改浏览器的User-Agent来伪装你的浏览器和操作系统
2011/07/29 PHP
PHP fgetcsv 定义和用法(附windows与linux下兼容问题)
2012/05/29 PHP
解决php使用异步调用获取数据时出现(错误c00ce56e导致此项操作无法完成)
2013/07/03 PHP
模板引擎smarty工作原理以及使用示例
2014/05/25 PHP
php实现的双向队列类实例
2014/09/24 PHP
thinkphp3.0输出重复两次的解决方法
2014/12/19 PHP
PHP数据库操作三:redis用法分析
2017/08/16 PHP
悄悄用脚本检查你访问过哪些网站的代码
2010/12/04 Javascript
js 实现图片预加载(js操作 Image对象属性complete ,事件onload 异步加载图片)
2011/03/25 Javascript
js完美解决IE6不支持position:fixed的bug
2015/04/24 Javascript
nodejs实现获取当前url地址及url各种参数值
2015/06/25 NodeJs
javaScript实现可缩放的显示区效果代码
2015/10/26 Javascript
浅谈JavaScript中小数和大整数的精度丢失
2016/05/31 Javascript
js不间断滚动的简单实现
2016/06/03 Javascript
原生js获取iframe中dom元素--父子页面相互获取对方dom元素的方法
2016/08/05 Javascript
Vue和React组件之间的传值方式详解
2019/01/31 Javascript
[53:21]2014 DOTA2国际邀请赛中国区预选赛5.21 DT VS LGD-CDEC
2014/05/22 DOTA
在Docker上部署Python的Flask框架的教程
2015/04/08 Python
python引用(import)某个模块提示没找到对应模块的解决方法
2019/01/19 Python
浅析Python 读取图像文件的性能对比
2019/03/07 Python
python3.6根据m3u8下载mp4视频
2019/06/17 Python
Python+threading模块对单个接口进行并发测试
2019/06/25 Python
应用服务器有那些
2012/01/19 面试题
护理专业应届毕业生推荐信
2013/11/15 职场文书
优良学风班总结材料
2014/02/08 职场文书
入股协议书
2014/04/14 职场文书
高中班主任评语
2014/12/30 职场文书
资料员岗位职责
2015/02/10 职场文书
2015年医德医风工作总结
2015/04/02 职场文书
员工工作心得体会
2019/05/07 职场文书
Redis 的查询很快的原因解析及Redis 如何保证查询的高效
2022/03/16 Redis
Python用tkinter实现自定义记事本的方法详解
2022/03/31 Python
宝塔更新Python及Flask项目的部署
2022/04/11 Python