12
16

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.

Python+SeleniumでスクレイピングしてApple整備済製品をカートに入れ、ラインで通知する

Posted at

##はじめに
Mac等を定価より安く買う方法として、認定整備済製品は選択肢の一つとしてなりえます。
新品同様の製品を約15%オフの値段で購入できますが、人気商品であればすぐに売り切れてしまいます。
機会を逃さないために、プログラミングで希望の商品を検索してカートに追加、ラインで通知するまでをプログラミングで実装しました。

##実際のコード
###本体
目的のページにアクセス→指定した文字列が含まれるURLにジャンプ→ログイン処理→カートに入れる→ラインで通知→商品名を保存、の流れです。
Seleniumに関する説明は多々あるので、この記事では割愛させて頂きます。
Selenium
hogeは実際の文字に置き換えてください。

AppleUsedSearch.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from line_notify_bot import LINENotifyBot

bot = LINENotifyBot(access_token='hoge')

options = webdriver.ChromeOptions()

options.add_argument('--headless')
driver = webdriver.Chrome(options=options) 

driver.get('https://www.apple.com/jp/shop/refurbished/mac/2019') 

r = driver.find_elements_by_class_name('as-producttile-tilelink')
len = len(r)
list =[]
for i in range(len):
    r = driver.find_elements_by_class_name('as-producttile-tilelink')[i].get_attribute('data-display-name')
    list.append(r)
    if "15.4インチMacBook Pro 2.2GHz" in r and "スペースグレイ" in r:
        detail = driver.find_elements_by_class_name('as-producttile-tilelink')[i].get_attribute("href")
        driver.get(detail)
        if driver.find_elements_by_class_name('ac-gn-bagview-nav-link ac-gn-bagview-nav-link-signIn'):
            login = driver.find_element_by_class_name('ac-gn-bagview-nav-link ac-gn-bagview-nav-link-signIn').get_attribute("href")
            driver.get('login')
            driver.find_element_by_id("loginHome.customerLogin.appleId-label").send_keys("hogehoge@gmail.com")
            driver.find_element_by_id("loginHome.customerLogin.password-label").send_keys("hogehogehoge")
            driver.find_element_by_id("signin-button-submit").click()
        driver.find_element_by_name("add-to-cart").click()
        bot.send(
            message='入荷有り'+r
        )
        driver.get('https://www.apple.com/jp/shop/refurbished/mac/2019') 
driver.quit()

print(list)
import pandas as pd
df = pd.Series(list)
df.to_csv("Item.csv", encoding='utf_8_sig')

###ライン通知
先人が素晴らしいコードを書かれていたので、僭越ながら使わせていただきました。
PythonでLINEにメッセージを送る

line_notify_bot.py
import requests

class LINENotifyBot:
    API_URL = 'https://notify-api.line.me/api/notify'
    def __init__(self, access_token):
        self.__headers = {'Authorization': 'Bearer ' + access_token}

    def send(
            self, message,
            image=None, sticker_package_id=None, sticker_id=None,
            ):
        payload = {
            'message': message,
            'stickerPackageId': sticker_package_id,
            'stickerId': sticker_id,
            }
        files = {}
        if image != None:
            files = {'imageFile': open(image, 'rb')}
        r = requests.post(
            LINENotifyBot.API_URL,
            headers=self.__headers,
            data=payload,
            files=files,
            )

##まとめ
サイドプロジェクトの初投稿ということで、自分が興味を持っているスクレイピングについて勉強したことを書きました。
定期実行できていない点や、アクセスコードを直接記述している点は修正に取り組みます。
日々コーディングして、情報発信していきます。

12
16
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
12
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?