2
0

アスクルのサイトの購入履歴取得〜クローリング〜

Last updated at Posted at 2024-01-08

仕事で使用しているアスクルの購入履歴をCSVに保存するため、まず該当ページのhtmlをローカルに保存するコードを書きました。

crwaling.py

import os
from time import sleep

from selenium.webdriver.common.by import By

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
#Seleniumで使用するドライババージョンアップ自動化
from webdriver_manager.chrome import ChromeDriverManager

from selenium.common.exceptions import NoSuchElementException

# Selenium初期化
def initialize():
   new_driver = ChromeDriverManager().install()
   service  = ChromeService(executable_path=new_driver)
   
   options = webdriver.ChromeOptions()
   options.add_argument('--incognito')
#    options.add_argument('--headless')

   return webdriver.Chrome(service=service, options=options)

driver = initialize()

driver.implicitly_wait(10)

driver.get('https://solution.soloel.com/')
sleep(3)

#IDの入力
account_id = driver.find_element(By.ID, 'loginId')
account_id.send_keys(input('IDを入力:'))
sleep(3)

#IDの入力
account_pw = driver.find_element(By.ID, 'password')
account_pw.send_keys(input('パスワードを入力:'))
sleep(3)

#ログイン
login = driver.find_element(By.ID, 'doLogin')
login.click()
sleep(5)

#履歴のページに移動
driver.get('https://solution.soloel.com/buyer/view/a010/DAG01010.html_xxxxxx')
sleep(5)

from_date_input = driver.find_element(By.ID, "serchFromDate")
from_date_input.clear()
from_date_input.send_keys('2020/01/01')
sleep(3)

to_date_input = driver.find_element(By.ID, "serchToDate")
to_date_input.clear()
# 当月の月末を入力するため、まずシステム年月日を取得
today = datetime.date.today()
# システム年月の最終日を取得
lastDay = calendar.monthrange(today.year, today.month)[1]
# str型 にする
lastDate = str(datetime.date(today.year, today.month, lastDay))
#文字列を置き換え
lastDate= lastDate.replace('-','/')
to_date_input.send_keys(lastDate)
sleep(3)

goDoSearch = driver.find_element(By.ID, 'goDoSearch')
goDoSearch.click()

#Pythonファイルのあるディレクトリパス取得
dir_path = os.path.dirname(os.path.abspath(__file__))
html = driver.page_source

#dir_pathの中に,ファイル名{driver.title}.htmlで保存
p = os.path.join(dir_path, 'html', f'{driver.title}.html')
with open(p, 'w') as f:
    f.write(html)

next_page = driver.find_element(By.ID, "goPagerNext")

#htmlの名前にページ番号を振る準備
number = 1

#次ページに移動するボタンを取得。文字列が0の場合はブラウザを閉じる。
next_page = driver.find_element(By.ID, "goPagerNext")
s = len(str(next_page))
s = int(s)
if s == 0:
    driver.quit()

else:
    #ボタンがあればクリック。なければbreak
    while True:
        try:
            next_page = driver.find_element(By.ID, "goPagerNext")
            next_page.click()
            sleep(5)

            html = driver.page_source
            number += 1

            p = os.path.join(dir_path, 'html', f'{driver.title+(str(number))}.html')
            with open(p, 'w') as f:
                f.write(html)

        except NoSuchElementException:
            driver.quit()
            break
               

以下を追記修正しました。(2024年1月18日更新)

crwaling.py

#当月の月末を入力するため以下をインポート
import calendar
import datetime

# まずシステム年月日を取得
today = datetime.date.today()
# システム年月の最終日を取得
lastDay = calendar.monthrange(today.year, today.month)[1]
# str型 にする
lastDate = str(datetime.date(today.year, today.month, lastDay))
#文字列を置き換え
lastDate= lastDate.replace('-','/')
to_date_input.send_keys(lastDate)
sleep(3)

保存したhtmlから情報をCSVに書き込むコードは、以下の記事をご覧ください。

2
0
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
2
0