PowerShell/wgetやcurlのようにウェブページを取得する
の編集
Top
/
PowerShell
/
wgetやcurlのようにウェブページを取得する
[
トップ
] [
編集
|
差分
|
バックアップ
|
添付
|
リロード
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
-- 雛形とするページ --
(no template pages)
#navi(../) * PowerShellでwgetやcurlのようにウェブページを取得する方法 [#bdc9daef] Invoke-WebRequestコマンドレットを使用することにより、wgetのようにウェブページ(HTML)を簡単に取得することができます。~ 以下に実行例等を記します。 #contents #htmlinsertpcsp(win-top.html,win-sp.html) * 確認環境 [#z5b0a6b6] - 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 * 参考記事 [#j633ca25] -''PowerShell Scripting Weblog''~ --[[PowerShellでスクレイピング 後編 HTMLをパースする>http://winscript.jp/powershell/305]]~ -''Microsoft''~ --[[Invoke-WebRequest>https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6]] * Invoke-WebRequestを使ってウェブページを取得する [#tad9b337] ''Invoke-WebRequest''コマンドレットをすることにより、簡単にウェブページ(HTML)を取得することができます。~ ** オプションなしでInvoke-WebRequestを実行 [#fc12024a] 下記コマンドを実際に実行したときの出力です。 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をファイルに出力する場合 [#f832a5fd] ファイルに出力したい場合は、以下のようになります。 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パラメータ [#sc68424f] Invoke-WebRequestの-UseBasicParsingを使うとブラウザエンジンを使わず、一般的なパースのみを行うそうです。~ 参考記事~ -[[PowerShellでスクレイピング 後編 HTMLをパースする>http://winscript.jp/powershell/305]]~ 上記の参考記事を参考にさせて頂きますと、AllElements、Forms、Scriptsのプロパティ値が取得できないようです。~ もし、HTML以外の要素で上行のプロパティ必要とする場合は(ブラウザエンジンによるパースを利用する場合)、-UseBasicParsingを~ 付与しないでください。 #htmlinsertpcsp(win-btm.html,win-sp.html)
タイムスタンプを変更しない
#navi(../) * PowerShellでwgetやcurlのようにウェブページを取得する方法 [#bdc9daef] Invoke-WebRequestコマンドレットを使用することにより、wgetのようにウェブページ(HTML)を簡単に取得することができます。~ 以下に実行例等を記します。 #contents #htmlinsertpcsp(win-top.html,win-sp.html) * 確認環境 [#z5b0a6b6] - 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 * 参考記事 [#j633ca25] -''PowerShell Scripting Weblog''~ --[[PowerShellでスクレイピング 後編 HTMLをパースする>http://winscript.jp/powershell/305]]~ -''Microsoft''~ --[[Invoke-WebRequest>https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6]] * Invoke-WebRequestを使ってウェブページを取得する [#tad9b337] ''Invoke-WebRequest''コマンドレットをすることにより、簡単にウェブページ(HTML)を取得することができます。~ ** オプションなしでInvoke-WebRequestを実行 [#fc12024a] 下記コマンドを実際に実行したときの出力です。 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をファイルに出力する場合 [#f832a5fd] ファイルに出力したい場合は、以下のようになります。 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パラメータ [#sc68424f] Invoke-WebRequestの-UseBasicParsingを使うとブラウザエンジンを使わず、一般的なパースのみを行うそうです。~ 参考記事~ -[[PowerShellでスクレイピング 後編 HTMLをパースする>http://winscript.jp/powershell/305]]~ 上記の参考記事を参考にさせて頂きますと、AllElements、Forms、Scriptsのプロパティ値が取得できないようです。~ もし、HTML以外の要素で上行のプロパティ必要とする場合は(ブラウザエンジンによるパースを利用する場合)、-UseBasicParsingを~ 付与しないでください。 #htmlinsertpcsp(win-btm.html,win-sp.html)
テキスト整形のルールを表示する