LoginSignup
12
15

More than 5 years have passed since last update.

PowerShellでwebスクレイピング

Last updated at Posted at 2014-08-09

やること

PowerShellでGoogle Trendから最新のトレンドをぶっこぬいてくる

依存

コード

Add-Type -AssemblyName System.Net.Http
Add-Type -Path "SgmlReaderDll.dllへのパス"


$hc = New-Object System.Net.Http.HttpClient
$stream = $hc.GetAsync("https://www.google.co.jp/trends/").Result.Content.ReadAsStreamAsync().Result

$my_streamreader = New-Object System.IO.StreamReader($stream, [System.Text.Encoding]::UTF8)
$my_sgml = New-Object Sgml.SgmlReader
$my_sgml.DocType = "HTML"
$my_sgml.IgnoreDtd = $true
$my_sgml.InputStream = $my_streamreader
$my_xdocument = [Xml.Linq.XDocument]::Load($my_sgml)
$data = [Xml]($my_xdocument)
$navi = $data.CreateNavigator()


$navi.Select('//*[contains(concat(" ", normalize-space(@class), " "), " hottrends-single-trend-title ")]') | %{
    Write-Host $_
}

今後の課題

  • PowerShellにおいて、async/awaitを前提としたライブラリはどのように呼んでやるべきか
  • Invoke-WebRequest版も書く[追記:書きました

参考URL

12
15
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
12
15