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