[PYTHON] Dragon Quest 10 bazaar market price acquisition program created in 20 minutes

Introduction

I am working for Dragon Quest 10 while refraining from doing so. Dragon Quest 10 has a mechanism called a traveler bazaar, which allows users to buy and sell items. So, I wrote a simple code to get the price information of the traveler bazaar from the adventurer's square and paste it on the Excel sheet.

environment

Windows10 Pro

Python 3.8.2

selenium 3.141.0

ChromeDriver 81.0.4044.69

openpyxl 3.0.3

code

config.py


USERID = 'User ID'
PASSWORD = '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

#List to store listing information
exhibition_data = []

options = webdriver.ChromeOptions()

#Headless mode
#options.add_argument('--headless')

#Specify the webdriver path
driver = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=options)
#Search destination URL
driver.get('https://hiroba.dqx.jp/sc/search/')

time.sleep(3)

#Search window input
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)

#Click login button
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()

#Search
time.sleep(2)

while True:

    print("Please enter the item name to search for:")
    #Stores the item name to search
    search_word = input()

    #Exit if nothing is entered
    if search_word == "":
        print("Exit the program")
        break
    
    #Fill in the search form
    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)

    #Acquisition of listing data
    elements = driver.find_elements_by_tag_name('tr')
    
    #Store in list
    for elem in elements:
        exhibition_data.append(elem.text.split())
    
    #Removed the first 5 useless elements
    del exhibition_data[:6]

    workbook = openpyxl.Workbook()
    sheet = workbook.active
    
    #Output to Excel sheet
    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]
    
    #Reset the contents of the list once
    del exhibition_data[:]

    #Save Excel file
    workbook.save(search_word + "_" + datetime.datetime.now().strftime("%Y%m%d%H%M") + '.xlsx')

    #Close workbook
    workbook.close()

#Close the driver
driver.quit()

Task

Since it is a simple code, it is designed to get only the first page of the result, and there is no error handling, so I would like to add it when using it.

Recommended Posts

Dragon Quest 10 bazaar market price acquisition program created in 20 minutes
CSS environment created in 10 minutes using Django