获取苹果想要的翻新产品代码(python)

我想在苹果官网买一个官翻机,我想买的是型号是:

翻新 14 英寸 MacBook Pro Apple M3 Pro 芯片 (配备 11 核中央处理器和 14 核图形处理器) - 深空黑色 36G + 1TB

价格是18199,由于官网这个官翻机上新时间不固定,每次都要手动去苹果官网看有没有上架我想要的型号,很麻烦,所以我在网上找了点代码改了一下,实现了让python程序去帮我看的方法,该程序每隔10分钟执行一次请求,去看官网有没有我想要的型号,有的话就会生成一张图片出来提示我(可以考虑改成发邮件的那种),同时将相似型号的机器的信息记录进文件里,功能比较简单。

import requests
from bs4 import BeautifulSoup
import schedule
import time
import imageio.v2 as imageio
import matplotlib.pyplot as plt
import re

fig = plt.figure()
f1 = fig.add_subplot(121)
isbuy = True
call_count = 1


def http_get_request():
    global call_count
    global prices
    # 下面要替换成你自己的实际文件地址
    f = open("/Users/Desktop/macbookpro.txt", "a", encoding="UTF-8")
    date_str = time.strftime('%Y-%m-%d  %H:%M:%S', time.localtime())
    f.write(f'{date_str}||---------第{call_count}次查询----------\n')
    call_count += 1
    ala = []
    try:
        request_url = 'https://www.apple.com.cn/shop/refurbished/mac/macbook-pro-36gb'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
        }
        response = requests.get(request_url, headers=headers, timeout=5)
        response.encoding = 'utf-8'
        htm = response.text
        soup = BeautifulSoup(htm, 'html.parser')
        # print(f"this is soupDemo:{soup}")
        # print('------------------------------------------------------------------------------')
        nparent = soup.find("div", attrs={"class": "rf-refurb-category-grid-no-js"})
        # print(f"this is npDemo:{nparent}")
        # print('------------------------------------------------------111-------')
        ala = nparent.find_all('a')
        prices = nparent.find_all("div", attrs={"class": "as-price-currentprice as-producttile-currentprice"})
        # print(f"this is priceDemo{prices}")
    except Exception:
        print('http请求超时')
    for i in range(len(ala)):
        name = str(ala[i].contents).strip('[]')
        price = str(prices[i].contents)
        match = re.search(r'RMB \d+,\d{3}', price)
        if match:
            price = match.group()
        # if "翻新 14 英寸 MacBook Pro Apple M3 Pro 芯片 (配备 12 核中央处理器和 18 核图形处理器) - 深空黑色" in name and "18,199" in price:
        # if "翻新 14 英寸 MacBook Pro Apple M3 Pro 芯片 (配备 12 核中央处理器和 18 核图形处理器) - 深空黑色" in name:
        if "翻新 14 英寸 MacBook Pro Apple M3 Pro 芯片 (配备 11 核中央处理器和 14 核图形处理器) - 深空黑色" in name:
            f.write(f"有型号为:{name}的电脑,价格为:{price}\n")
            if "18,199" in price:
                f.write(f"快下单!价格为:{price}\n")
                # 下面要替换成你自己的图片地址
                spect = imageio.imread('/User/Darth Vader.png')
                f1.imshow(spect)
                plt.show()
                isbuy = False
                continue
                pass
    f.write("\n")
    f.close()
    # div = soup.find_all('div', {'class':'rf-refurb-category-grid-no-js'})
    # print(div);
    # print('---------------------------------------------------------------------------')


if __name__ == '__main__':
    # 调用函数
    schedule.every(10).minutes.do(http_get_request)
    while isbuy:
        schedule.run_pending()
    time.sleep(1)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值