脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 用pushplus+python监控亚马逊到货动态推送微信

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

2021-08-31 00:23towelie Python

这篇文章主要介绍了用pushplus+python监控亚马逊到货动态推送微信的示例,帮助大家利用python抢购商品,感兴趣的朋友可以了解下

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

?
1
2
3
4
5
6
7
8
9
10
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源码

?
1
2
3
4
5
6
7
8
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源码,接下来只需要将其保存到文件,每隔一定时间再次爬虫比对就行了

 四、完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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监控亚马逊到货动态的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/feiyue666/p/14336870.html

延伸 · 阅读

精彩推荐