1
3

More than 3 years have passed since last update.

20分で作成したドラクエ10バザー相場取得プログラム

Posted at

はじめに

自粛中、ドラクエ10に勤しんでいます。
ドラクエ10には、旅人バザーという仕組みがあり、ユーザー同士でアイテムの売り買いが可能になっています。
そこで、冒険者の広場から旅人バザーの価格情報を取得し、Excelのシートに貼り付ける簡易的なコードを書きました。

環境

Windows10 Pro

Python 3.8.2

selenium 3.141.0

ChromeDriver 81.0.4044.69

openpyxl 3.0.3

コード

config.py
USERID = 'ユーザーID'
PASSWORD = 'パスワード'
bazaar.py
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import re
import config
import openpyxl
import datetime

# 出品情報を格納するリスト
exhibition_data = []

options = webdriver.ChromeOptions()

#ヘッドレスモード
#options.add_argument('--headless')

#webdriverのパスを指定
driver = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=options)
#検索先のURL
driver.get('https://hiroba.dqx.jp/sc/search/')

time.sleep(3)

#検索窓入力
s = driver.find_elements_by_xpath('//*[@id="sqexid"]')
s[0].send_keys(config.USERID)
s = driver.find_elements_by_xpath('//*[@id="password"]')
s[0].send_keys(config.PASSWORD)

# ログインボタンクリック
driver.find_element_by_xpath('//*[@id="login-button"]').click()
driver.find_element_by_xpath('//*[@id="welcome_box"]/div[2]/a').click()
driver.find_element_by_xpath('//*[@id="contentArea"]/div/div[2]/form/table/tbody/tr[2]/td[3]/a').click()

# 検索
time.sleep(2)

while True:

    print("検索するアイテム名を入力してください:")
    # 検索するアイテム名を格納
    search_word = input()

    # 何も入力されなければ終了
    if search_word == "":
        print("プログラムを終了します")
        break

    # 検索フォームに入力
    s = driver.find_elements_by_xpath('//*[@id="searchword"]')
    s[0].send_keys(search_word)

    driver.find_element_by_xpath('//*[@id="searchBoxArea"]/form/p[2]/input').click()
    driver.find_element_by_xpath('//*[@id="contentArea"]/div/div[4]/table/tbody/tr/th/table/tbody/tr/td[3]/a').click()
    time.sleep(3)
    driver.find_element_by_xpath('//*[@id="btn_lock"]/a').click()
    time.sleep(5)

    # 出品データの取得
    elements = driver.find_elements_by_tag_name('tr')

    # リストに格納
    for elem in elements:
        exhibition_data.append(elem.text.split())

    # 先頭に5つ入ってくる無駄な要素を削除
    del exhibition_data[:6]

    workbook = openpyxl.Workbook()
    sheet = workbook.active

    # Excelのシートへ出力
    for i in range(len(exhibition_data)):
        for j in range(len(exhibition_data[i])):

            sheet.cell(row=i + 1, column=j + 1).value = exhibition_data[i][j]

    # リストの中身を一旦リセット
    del exhibition_data[:]

    # Excelファイルを保存
    workbook.save(search_word + "_" + datetime.datetime.now().strftime("%Y%m%d%H%M") + '.xlsx')

    # ワークブックを閉じる
    workbook.close()

# ドライバーを閉じる
driver.quit()

課題

簡易的に書いたコードなので、結果の1ページ目しか取得しない仕様になっているのと、エラー処理もないので使用する時は、書き足したいとおもいます。

1
3
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
1
3