#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.6.1
Author: H. Tsubota

Script Function:
    Webサイトのテーブルからデータを取得してExcelシートに保存

#ce ----------------------------------------------------------------------------
#include <IE.au3>
#include <Excel.au3>

; Webからデータを取得
$oIE = _IECreate ("stocks.finance.yahoo.co.jp/stocks/history/?code=998407.O")
$oTable = _IETagNameGetCollection ( $oIE, "table", 1)
$oLines = _IETagNameGetCollection ( $oTable, "tr")

$oExcel = _ExcelBookNew()

; Webから取得したデータをExcelシートに書き込み
$iLines = $oLines.length -1
For $i=1 To $iLines    
    $oLine = $oLines($i)
    $oData = _IETagNameGetCollection ($oLine, "td")    
    _ExcelWriteCell($oExcel, $oData(0).innerText, $i, 1)
    _ExcelWriteCell($oExcel, $oData(1).innerText, $i, 2)
    _ExcelWriteCell($oExcel, $oData(2).innerText, $i, 3)
    _ExcelWriteCell($oExcel, $oData(3).innerText, $i, 4)
    _ExcelWriteCell($oExcel, $oData(4).innerText, $i, 5)
Next

; 平均を計算
$iAverageLine = $iLines+1
_ExcelWriteCell($oExcel, "平均値", $iAverageLine, 1)
_ExcelWriteFormula($oExcel, "=Average(R1C2:R"& $iLines &"C2)", $iAverageLine, 2)
_ExcelWriteFormula($oExcel, "=Average(R1C3:R"& $iLines &"C3)", $iAverageLine, 3)
_ExcelWriteFormula($oExcel, "=Average(R1C4:R"& $iLines &"C4)", $iAverageLine, 4)
_ExcelWriteFormula($oExcel, "=Average(R1C5:R"& $iLines &"C5)", $iAverageLine, 5)
_ExcelFontSetProperties($oExcel, $iAverageLine, 1, $iAverageLine, 5, True, True, True)

; 1行目に見出しを挿入
_ExcelRowInsert($oExcel, 1, 1)
_ExcelWriteCell($oExcel, "日付", 1, 1)
_ExcelWriteCell($oExcel, "始値", 1, 2)
_ExcelWriteCell($oExcel, "高値", 1, 3)
_ExcelWriteCell($oExcel, "安値", 1, 4)
_ExcelWriteCell($oExcel, "終値", 1, 5)

; Bookを保存して閉じる
_ExcelBookSaveAs($oExcel, @ScriptDir & "\Data", "xls")
_ExcelBookClose($oExcel)

_IEQuit($oIE)

Blog <実験記録 No.02>.