使用 Python 和 WayScript 构建亚马逊价格追踪器
大家
好,今天我们将讨论如何通过使用WayScript抓取价格详情和调度来构建亚马逊价格追踪器
让我来告诉你我做这个的真正原因 ;)
我是个吃货,超爱 Nutella 🤤 但由于疫情,我附近任何一家门店都找不到 Nutella。所以我想在亚马逊上订购,但价格实在太高,而且波动很大……于是我开始做一个追踪器,每当价格下降时就会通知我。
让我们开始吧..
我们将创建一个 Python 脚本,该脚本使用 request 和 beautifulSoup 模块来抓取数据,并安排脚本每小时运行一次。可以使用无服务器 AWS Lambda或WayScript来实现,但我不太喜欢 AWS Lambda。所以我使用了 WayScript。
我们将分两个阶段讨论这个过程。
第一阶段:抓取产品详细信息;
第二阶段:安排脚本每小时运行一次。
第一阶段:
第 2 步:导入必要的包/模块
import requests
import bs4
import pandas as pd
请求- 请求模块允许您使用 Python
bs4发送 HTTP 请求- Beautiful Soup 是一个可以轻松从网络
pandas中抓取信息的库- pandas 是一种快速且功能强大的数据分析工具。
我们假装自己是 Firefox 用户以避免限制。
HEADERS = ({'User-Agent':
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
'Accept-Language': 'en-US, en;q=0.5'})
步骤 3:构建 Python 脚本
这是跟踪器函数,它接受参数 URL 和 TrackingPrice,并在其小于或等于 TrackingPrice 时添加产品的详细信息
def tracker(url,TrackingPrice):
res = requests.get(url,headers=HEADERS)
soup = bs4.BeautifulSoup(res.content, features='lxml')
# to prevent script from crashing when there isn't a price for the product
try:
title = soup.find(id="productTitle").get_text().strip()
amount = float(soup.find(id='priceblock_ourprice').get_text().replace("₹","").replace("$","").strip())
if amount<=TrackingPrice:
offer.append("You got a offer on the {0} for {1}. Check out the product {2}".format(title,amount,url))
except:
offer.append("Couldn't get details about product")
源代码:
import requests
import bs4
import pandas as pd
HEADERS = ({'User-Agent':
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
'Accept-Language': 'en-US, en;q=0.5'})
offer=[]
def tracker(url,TrackingPrice):
res = requests.get(url,headers=HEADERS)
soup = bs4.BeautifulSoup(res.content, features='lxml')
# to prevent script from crashing when there isn't a price for the product
try:
title = soup.find(id="productTitle").get_text().strip()
amount = float(soup.find(id='priceblock_ourprice').get_text().replace("₹","").replace("$","").strip())
if amount<=TrackingPrice:
offer.append("You got a offer on the {0} for {1}. Check out the product {2}".format(title,amount,url))
except:
offer.append("Couldn't get details about product")
df=pd.read_csv("https://docs.google.com/spreadsheets/d/1AzJ93zR6--4vwl81W3v0FHyZ_bFMkFYRxOSjodJu_Qw/export?format=csv")
for i in range(0,len(df["URL"])):
tracker(df["URL"][i],df["TrackingPrice"][i])
outputs["message"]=offer
输出:
我们已经完成了 Python 脚本,
现在让我们进入第二阶段
第二阶段:
我已经说过,你可以使用 AWS Lambda 或任何其他云计算服务来调度这个脚本,因为我对 AWS 不太熟悉。我打算使用 WayScript
并且此 WayScript 提供了广泛的包来与服务交互,
例如:
- Gmail
- 短信
- 图表
- Figma
- 松弛
- Trello
- 叽叽喳喳
步骤 1:打开用于此调度的WayScript包:
- 时间触发器
- Python
- 环形
- 短信
第二步:在博客里解释所有内容是不可能的,而且会让人觉得很麻烦。所以我制作了“如何在 WayScript 中配置 Python 脚本”的视频。
请按照视频操作。
在一个幸运的日子里..
价格降低了,这个脚本通过短信通知了我,我就订购了😁如果您喜欢我的内容,请考虑支持我
希望它有用
A❤️会很棒😊