このエントリーをはてなブックマークに追加


PowerShellでwgetやcurlのようにウェブページを取得する方法

Invoke-WebRequestコマンドレットを使用することにより、wgetのようにウェブページ(HTML)を簡単に取得することができます。
以下に実行例等を記します。


確認環境

  • PowerShellバージョン
    PS C:\> $PSVersionTable.PSVersion
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      1      16299  251
  • OS(Windows10)
    PS C:\> (Get-WmiObject Win32_OperatingSystem).Version
    10.0.16299

参考記事

Invoke-WebRequestを使ってウェブページを取得する

Invoke-WebRequestコマンドレットをすることにより、簡単にウェブページ(HTML)を取得することができます。

オプションなしでInvoke-WebRequestを実行

下記コマンドを実際に実行したときの出力です。

Invoke-WebRequest URL
PS C:\> Invoke-WebRequest http://just4fun.biz

StatusCode        : 200
StatusDescription : OK
Content           : <html lang="ja">
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>www.just4fun.biz</title>
                    <style type="text/css">
                    html, body {
                      width: 100%;
                      height: 100%;
                      margin:...
RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    Vary: Accept-Encoding
                    Accept-Ranges: bytes
                    Content-Length: 2294
                    Content-Type: text/html
                    Date: Tue, 17 Apr 2018 03:31:31 GMT
                    Last-Modified: Fri, 14 Jul 201...
Forms             : {}
Headers           : {[Connection, keep-alive], [Vary, Accept-Encoding], [Accept-Ranges, bytes], [Content-Length, 2294].
                    ..}
Images            : {@{innerHTML=; innerText=; outerHTML=<IMG src="http://linux.just4fun.biz/image/logo.png">; outerTex
                    t=; tagName=IMG; src=http://linux.just4fun.biz/image/logo.png}, @{innerHTML=; innerText=; outerHTML
                    =<IMG src="http://win.just4fun.biz/image/logo.png">; outerText=; tagName=IMG; src=http://win.just4f
                    un.biz/image/logo.png}, @{innerHTML=; innerText=; outerHTML=<IMG src="http://db.just4fun.biz/image/
                    logo.png">; outerText=; tagName=IMG; src=http://db.just4fun.biz/image/logo.png}, @{innerHTML=; inne
                    rText=; outerHTML=<IMG src="http://cryptocurrency.just4fun.biz/image/logo.png">; outerText=; tagNam
                    e=IMG; src=http://cryptocurrency.just4fun.biz/image/logo.png}...}
InputFields       : {}
Links             : {@{innerHTML=<IMG src="http://linux.just4fun.biz/image/logo.png"> ; innerText= ; outerHTML=<A href=
                    "http://linux.just4fun.biz"><IMG src="http://linux.just4fun.biz/image/logo.png"> </A>; outerText= ;
                     tagName=A; href=http://linux.just4fun.biz}, @{innerHTML=Linuxと過ごす; innerText=Linuxと過
                    ごす; outerHTML=<A href="http://linux.just4fun.biz">Linuxと過ごす</A>; outerText=Linuxとé
                    Žã”す; tagName=A; href=http://linux.just4fun.biz}, @{innerHTML=<IMG src="http://win.just4fun.biz/
                    image/logo.png"> ; innerText= ; outerHTML=<A href="http://win.just4fun.biz"><IMG src="http://win.ju
                    st4fun.biz/image/logo.png"> </A>; outerText= ; tagName=A; href=http://win.just4fun.biz}, @{innerHTM
                    L=Windowsと暮らす; innerText=Windowsと暮らす; outerHTML=<A href="http://win.just4fun.biz"
                    >Windowsと暮らす</A>; outerText=Windowsと暮らす; tagName=A; href=http://win.just4fun.biz}
                    ...}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 2294

以下の通り、HtmlWebResponseObjectが返却されているのが確認できます。

PS C:\> (Invoke-WebRequest http://just4fun.biz).GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    HtmlWebResponseObject                    Microsoft.PowerShell.Commands.WebResponseObject

以下のようにメンバーを指定すれば、HTTPステータスなども取得できます。

PS C:\> $html=(Invoke-WebRequest http://just4fun.biz)
PS C:\> $html.StatusCode
200
PS C:\> $html.RawContentLength
2294

取得したHTMLをファイルに出力する場合

ファイルに出力したい場合は、以下のようになります。

Invoke-WebRequest URL -OutFile ファイル名

実際に実行したときの出力は以下の通りです。

PS C:\Users\sakura\Desktop> Invoke-WebRequest http://just4fun.biz -OutFile html.txt
PS C:\Users\sakura\Desktop> Get-ChildItem .\html.txt

    ディレクトリ: C:\Users\sakura\Desktop

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018/04/17     12:37           2294 html.txt

ファイルに出力されているのが確認できます。

-UseBasicParsingパラメータ

Invoke-WebRequestの-UseBasicParsingを使うとブラウザエンジンを使わず、一般的なパースのみを行うそうです。

参考記事

上記の参考記事を参考にさせて頂きますと、AllElements、Forms、Scriptsのプロパティ値が取得できないようです。
もし、HTML以外の要素で上行のプロパティ必要とする場合は(ブラウザエンジンによるパースを利用する場合)、-UseBasicParsingを
付与しないでください。



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-04-17 (火) 12:47:51