使用Python的亚马逊产品可用性检查器
众所周知, Python是一种多用途语言,广泛用于脚本编写。它的用途不仅限于解决复杂的计算,还可以自动化日常生活任务。假设我们想要跟踪任何亚马逊产品的可用性,并在产品可用性发生变化时抓住交易,并通过电子邮件通知用户可用性。为此编写Python脚本将非常有趣。
注意:在运行脚本之前安装所需的库(根据代码)。另外,请注意如果产品当前不可用,则不会向用户发送电子邮件。用户应该为他想要跟踪的产品提供Asin Id 。
Working of each module used:
-> requests: Used to make HTTP get and post requests
-> time: Used to find current time, wait, sleep
-> schedule: Used to schedule a function to run again after intervals. It is similar to “setInterval” functionality in JavaScript.
-> smptlib: Used to send email using Python.
下面是上述项目的实现:
Python3
# Python script for Amazon product availability checker
# importing libraries
from lxml import html
import requests
from time import sleep
import time
import schedule
import smtplib
# Email id for who want to check availability
receiver_email_id = "EMAIL_ID_OF_USER"
def check(url):
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36'}
# adding headers to show that you are
# a browser who is sending GET request
page = requests.get(url, headers = headers)
for i in range(20):
# because continuous checks in
# milliseconds or few seconds
# blocks your request
sleep(3)
# parsing the html content
doc = html.fromstring(page.content)
# checking availability
XPATH_AVAILABILITY = '//div[@id ="availability"]//text()'
RAw_AVAILABILITY = doc.xpath(XPATH_AVAILABILITY)
AVAILABILITY = ''.join(RAw_AVAILABILITY).strip() if RAw_AVAILABILITY else None
return AVAILABILITY
def sendemail(ans, product):
GMAIL_USERNAME = "YOUR_GMAIL_ID"
GMAIL_PASSWORD = "YOUR_GMAIL_PASSWORD"
recipient = receiver_email_id
body_of_email = ans
email_subject = product + ' product availability'
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login(GMAIL_USERNAME, GMAIL_PASSWORD)
# message to be sent
headers = "\r\n".join(["from: " + GMAIL_USERNAME,
"subject: " + email_subject,
"to: " + recipient,
"mime-version: 1.0",
"content-type: text/html"])
content = headers + "\r\n\r\n" + body_of_email
s.sendmail(GMAIL_USERNAME, recipient, content)
s.quit()
def ReadAsin():
# Asin Id is the product Id which
# needs to be provided by the user
Asin = 'B077PWK5BT'
url = "http://www.amazon.in/dp/" + Asin
print ("Processing: "+url)
ans = check(url)
arr = [
'Only 1 left in stock.',
'Only 2 left in stock.',
'In stock.']
print(ans)
if ans in arr:
# sending email to user if
# in case product available
sendemail(ans, Asin)
# scheduling same code to run multiple
# times after every 1 minute
def job():
print("Tracking....")
ReadAsin()
schedule.every(1).minutes.do(job)
while True:
# running all pending tasks/jobs
schedule.run_pending()
time.sleep(1)
输出:
Tracking....
Processing: http://www.amazon.in/dp/B077PWK5BT
Only 1 left in stock.
Tracking....
Processing: http://www.amazon.in/dp/B077PWK5BT
Only 1 left in stock.
Tracking....
Processing: http://www.amazon.in/dp/B077PWK5BT
Only 1 left in stock.
请注意,程序在向用户发送邮件时可能会抛出错误(严重安全警报/登录尝试被阻止),这可以通过修改您正在使用的邮件应用程序中的安全设置来处理。