7
15

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 1 year has passed since last update.

【Python】AmazonAPIを使って自分が欲しい商品の値下げをLINEに通知

Last updated at Posted at 2022-07-06

【ソースコード】

import requests
from bs4 import BeautifulSoup # データの抽出を行うためのライブラリ
import time

url = "AmazonのURL"

# トラッキングの設定
def amazon_tracking_price():
    amazon_page = requests.get(url)
    soup = BeautifulSoup(amazon_page.content, "html.parser") # BeautifulSoup(調べたい内容:Amazonページのコントテンツの内容,調べ方:html形式で解析)
    # print(soup)

    title = soup.find(id = "productTitle").get_text() # find(ここの設定は"find()の中身の決め方"を参考)
    price = soup.find("span", class_="a-price-whole").get_text().replace(',', '')
    print(title)
    print(price)
    
    # 通知する価格を設定
    if int(price) < 8000:
        send_line_notify(price)

# LINEに通知    
def send_line_notify(price):
    print("lineに通知がいきした")
    line_notify_token = "発行した自分のトークンをここにペースト"
    line_notify_api = "https://notify-api.line.me/api/notify" # LINEへの通知のためのAPI
    notification_message = "価格:" + price + "円になりました." + url # 通知メッセージの内容
    headers = {"Authorization": f"Bearer {line_notify_token}"}
    data = {"message": f'message: {notification_message}'}
    requests.post(line_notify_api, headers=headers, data=data)

while(True):
    print("トラッキングしました")
    time.sleep(60*60*24)
    amazon_tracking_price()

Requests, BeaufifulSoupのインストール

下記のコマンドをコマンドプロンプト(または,ターミナル)から実行することでimportが可能になる

pip install requests
pip install beautifulsoup4

find()の中身の決め方

  1. 商品ページに行きディベロッパーページ[F12]を開く.
  2. 下記の画像の赤丸のアイコンをクリック
  3. カーソルを価格のところに合わせるとそこに対応するhtmlのコードがハイライトされる
  4. ハイライトされたコードのclass名 or id名 をコピー
  5. find() の中にペースト
    スクリーンショット 2022-07-07 011229.png

LINEへの通知のためのAPI

下記のリンクにアクセスし,画像のURLをコピーしline_notify_api = ""に貼り付ける
https://notify-bot.line.me/doc/ja/
スクリーンショット 2022-07-07 012015.png

line_notify_tokenの発行

  1. 下記のページから自分のアカウントへログイン
  2. マイページへ移動
  3. アクセストークンの発行(開発者向け) から「トークンを発行する」を押す
  4. 発行されたトークンをコピーしてペースト
    https://notify-bot.line.me/ja/
    スクリーンショット 2022-07-07 012509.png

実行結果

Screenshot_20220707-013639.png

7
15
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
7
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?