Scraping with Python + Selenium to add Apple refurbished products to your cart and notify on line

Introduction

Certified refurbished product can be one of the options for buying a Mac etc. at a lower price than the list price. You can buy a product that looks like new at a price of about 15% off, but popular products sell out quickly. In order not to miss an opportunity, we have implemented programming to search for the desired product by programming, add it to the cart, and notify it by line.

Actual code

Body

Access the target page → Jump to the URL containing the specified character string → Login process → Add to cart → Notification by line → Save product name. There are many explanations about Selenium, so I will omit them in this article. Selenium Replace * hoge * with the actual character.

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-inch MacBook Pro 2.2GHz" in r and "Space gray" 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("[email protected]")
            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='In stock'+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')

Line notification

My ancestor wrote a wonderful code, so I used it while I was afraid. Send a message to LINE with Python

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,
            )

Summary

As the first post of the side project, I wrote that I learned about scraping that I am interested in. We will work on fixing the points that cannot be executed regularly and the points that the access code is written directly. We will code every day and send out information.

Recommended Posts

Scraping with Python + Selenium to add Apple refurbished products to your cart and notify on line
Automate Chrome with Python and Selenium on your Chromebook
Scraping with Python, Selenium and Chromedriver
Practice web scraping with Python and Selenium
Send a message to LINE with Python (LINE Notify)
Scraping with Selenium [Python]
Install selenium on Mac and try it with python
Automatic follow on Twitter with python and selenium! (RPA)
Scraping with selenium in Python
Scraping with Selenium + Python Part 1
Scraping with Selenium in Python
Linking Python and Arduino to display IME On / Off with LED
Python 3.6 on Windows ... and to Xamarin.
Scraping with Node, Ruby and Python
Scraping with Selenium in Python (Basic)
Scraping with Python and Beautiful Soup
Selenium and python to open google
Install Chrome on the command line on Sakura VPS (Ubuntu) and launch Chrome with python from virtual display and selenium
Use AWS lambda to scrape the news and notify LINE of updates on a regular basis [python]
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
[Yahoo! Weather Replacement Version] How to get weather information with LINE Notify + Python
Connect to VPN with your smartphone and turn off / on the server
Add 95% confidence intervals on both sides to the diagram with Python / Matplotlib
How to draw a vertical line on a heatmap drawn with Python seaborn
Fractal to make and play with Python
A memo with Python2.7 and Python3 on CentOS
[Part1] Scraping with Python → Organize to csv!
Add Gaussian noise to images with python2.7
Easy web scraping with Python and Ruby
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
WEB scraping with python and try to make a word cloud from reviews
Want to add type hints to your Python decorator?
MessagePack-Try to link Java and Python with RPC
I tried web scraping using python and selenium
Strategy on how to monetize with Python Java
Try running Google Chrome with Python and Selenium
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Log in to Yahoo Business with Selenium Python
Drag and drop local files with Selenium (Python)
Introduction to Python with Atom (on the way)
Notify error and execution completion by LINE [Python]
Connect to MySQL with Python on Raspberry Pi
I tried to automate the article update of Livedoor blog with Python and selenium.
Python> Implementation that synthesizes 3 files with variable columns and outputs 3 items on each line
I tried various things with Python: scraping (Beautiful Soup + Selenium + PhantomJS) and morphological analysis.