import requests
from bs4 import BeautifulSoup
import subprocess
import schedule
import time
daily_loop = True
# Function: Track refurbished product
def track_refurbished_products(model: str, chip: str, price: float, product="mac", memory="16gb", refurbised="整備済製品"):
global daily_loop
# Request Configuration
url = f"https://www.apple.com/jp/shop/refurbished/{product}/{memory}"
# Reference: http://myhttpheader.com/
headers = {
"User-Agent": "Mozilla/5.0...",
"Accept-Language": "ja,en-US;q=0.9,en;q=0.8"
}
response = requests.get(url, headers=headers).content
# print(response)
soup = BeautifulSoup(response, "html.parser")
# print(soup.prettify())
li_tags = soup.find_all(name="li")
# print(li_tags)
product_list = []
for li_tag in li_tags:
if model in li_tag.text and chip in li_tag.text and refurbised in li_tag.text:
# print(li_tag)
product_info = li_tag.getText().split()
# print(product_info)
product_name = " ".join(product_info[:-2])
# print(product_name)
product_price = product_info[-1]
# print(product_price)
price_float = float(''.join(product_price.split("円")[0].split(",")))
# print(price_float)
# product_url = li_tag.select_one("div a")["href"]
# print(product_url)
if price_float < price:
product_list.append(product_name + ": " + product_price + "\n\n")
if product_list != []:
print("Some products have been found!")
product_list_str = "".join(product_list)
applescript: str = f"""
display alert "{product_list_str}"
"""
# print(applescript)
subprocess.call("osascript -e '{}'".format(applescript), shell=True)
daily_loop = False
if __name__ == "__main__":
schedule.every(1).minutes.do(track_refurbished_products, model="MacBook", chip="M1", price=150000, product="mac", memory="16gb", refurbised="整備済製品")
while daily_loop:
schedule.run_pending()
time.sleep(1)
More than 3 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme