Skip to content

Instantly share code, notes, and snippets.

@rirufa
Created October 12, 2020 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rirufa/2624d3a5b464d3beb30199c4bc119e74 to your computer and use it in GitHub Desktop.
Save rirufa/2624d3a5b464d3beb30199c4bc119e74 to your computer and use it in GitHub Desktop.
Yahoo株価の時系列からTSVを取得する
[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)
@rirufa
Copy link
Author

rirufa commented Oct 12, 2020

https://www.ka-net.org/blog/?p=12517

導入の仕方はこのサイトを参照してください

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment