このPowerShellスクリプトでは、htmlタグを削除してテキストを残し、スクリプトの実行時にそのhtmlファイルの語数を表示させます。powershellを使用してExcelスプレッドシートにエクスポートするには
function Html-ToText {
param([System.String] $html)
# remove line breaks, replace with spaces
$html = $html -replace "(`r|`n|`t)", " "
# write-verbose "removed line breaks: `n`n$html`n"
# remove invisible content
@('head', 'style', 'script', 'object', 'embed', 'applet', 'noframes', 'noscript', 'noembed') | % {
$html = $html -replace "<$_[^>]*?>.*?</$_>", ""
}
# write-verbose "removed invisible blocks: `n`n$html`n"
# Condense extra whitespace
$html = $html -replace "()+", " "
# write-verbose "condensed whitespace: `n`n$html`n"
# Add line breaks
@('div','p','blockquote','h[1-9]') | % { $html = $html -replace "</?$_[^>]*?>.*?</$_>", ("`n" + '$0')}
# Add line breaks for self-closing tags
@('div','p','blockquote','h[1-9]','br') | % { $html = $html -replace "<$_[^>]*?/>", ('$0' + "`n")}
# write-verbose "added line breaks: `n`n$html`n"
#strip tags
$html = $html -replace "<[^>]*?>", ""
# write-verbose "removed tags: `n`n$html`n"
# replace common entities
@(
@("&bull;", " * "),
@("&lsaquo;", "<"),
@("&rsaquo;", ">"),
@("&(rsquo|lsquo);", "'"),
@("&(quot|ldquo|rdquo);", '"'),
@("&trade;", "(tm)"),
@("&frasl;", "/"),
@("&(quot|#34|#034|#x22);", '"'),
@('&(amp|#38|#038|#x26);', "&"),
@("&(lt|#60|#060|#x3c);", "<"),
@("&(gt|#62|#062|#x3e);", ">"),
@('&(copy|#169);', "(c)"),
@("&(reg|#174);", "(r)"),
@("&nbsp;", " "),
@("&(.{2,6});", "")
) | % { $html = $html -replace $_[0], $_[1] }
# write-verbose "replaced entities: `n`n$html`n"
return $html + $a | Measure-Object -word
}
をそして実行します:私は実行時に私の質問がある
のHTML ToText(新しいオブジェクトnet.webclient).DownloadString( "test.htmlという")を
それは、4を表示しますPowerShellの出力に表示されるという単語。 PowerShellウィンドウから出力をエクスポートするには、という単語と数のExcelスプレッドシートにエクスポートするにはどうすればよいですか?
私は使ってみました:$ x |オブジェクトの選択| Export-Csv out.csv -NoTypeInformationと吐き出される唯一のものは単語であってカウントではありませんでした。なぜそれが両方をしなかったのか分かりません。 –
それは私たちの2人を作る。 '' word1 word2 word3 word4 '| measure-object -Word |選択 - オブジェクトの単語| ConvertTo-Csv -NoTypeInformation' ... – TessellatingHeckler
は、.csvファイルではなくコンソールウィンドウに表示します。私はまだ単語を取得しますが、その下に整数はありません –