Created
October 12, 2020 12:38
-
-
Save rirufa/2624d3a5b464d3beb30199c4bc119e74 to your computer and use it in GitHub Desktop.
Yahoo株価の時系列からTSVを取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[void][Reflection.Assembly]::LoadFile("C:\edgedriver_win32\Selenium.WebDriver.4.0.0-alpha05\lib\net47\WebDriver.dll") #DLL読み込み | |
$msedge = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" #Edge本体のパス | |
$msedgedriverDir = "C:\edgedriver_win32" #Edge Driverがあるフォルダのパス | |
$msedgedriverExe = "msedgedriver.exe" #Edge Driver名 | |
$edgeOptions = New-Object OpenQA.Selenium.Edge.EdgeOptions | |
$edgeOptions.UseChromium = $true | |
$edgeOptions.BinaryLocation = $msedge | |
$service = [OpenQA.Selenium.Edge.EdgeDriverService]::CreateChromiumService($msedgedriverDir, $msedgedriverExe) | |
$service.EnableVerboseLogging = $false | |
$driver = New-Object OpenQA.Selenium.Edge.EdgeDriver($service, $edgeOptions) | |
$driver.Manage().Timeouts().ImplicitWait = [System.TimeSpan]::FromSeconds(5) #暗黙的な待機5秒 | |
$fileName = "test.tsv" | |
#以降ブラウザー操作 | |
$driver.Navigate().GoToUrl("https://info.finance.yahoo.co.jp/history/?code=9434.T") | |
do | |
{ | |
#CSVに変換する | |
$table = $driver.FindElementByXPath('//*[@id="main"]/div[5]/table') | |
$rows = $table.FindElements([OpenQA.Selenium.By]::TagName("tr")) | |
foreach($row in $rows){ | |
$cols = $row.FindElements([OpenQA.Selenium.By]::TagName("td")) | |
$csv_line = "" | |
foreach($col in $cols){ | |
$csv_line = $csv_line + $col.Text + "`t" | |
} | |
Write-Output $csv_line | Out-File -FilePath $fileName -append | |
} | |
try{ | |
$driver.FindElementByXPath('//*[@id="main"]/ul/a[text()="次へ"]').click() | |
}catch{ | |
break | |
} | |
} while($true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.ka-net.org/blog/?p=12517
導入の仕方はこのサイトを参照してください