0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

scraping refurbished

Posted at
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)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?