2016-11-05 10 views
-1

この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 
@( 
@("&amp;bull;", " * "), 
@("&amp;lsaquo;", "<"), 
@("&amp;rsaquo;", ">"), 
@("&amp;(rsquo|lsquo);", "'"), 
@("&amp;(quot|ldquo|rdquo);", '"'), 
@("&amp;trade;", "(tm)"), 
@("&amp;frasl;", "/"), 
@("&amp;(quot|#34|#034|#x22);", '"'), 
@('&amp;(amp|#38|#038|#x26);', "&amp;"), 
@("&amp;(lt|#60|#060|#x3c);", "<"), 
@("&amp;(gt|#62|#062|#x3e);", ">"), 
@('&amp;(copy|#169);', "(c)"), 
@("&amp;(reg|#174);", "(r)"), 
@("&amp;nbsp;", " "), 
@("&amp;(.{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スプレッドシートにエクスポートするにはどうすればよいですか?

答えて

0

したいCSVは、ちょうど次のようになります。

Words 
4 

それだけでExcelがそれを読んでます、テキストファイルにそれを書くのは簡単です。しかし、あなたは運が良かったです。Measure-Objectの出力は、すでに 'Words'をプロパティに、 '4'を値として持つオブジェクトで、Export-Csvに直接入力できます。私は私が試したの前に、HTMLの外の言葉を得るために

$x = Invoke-WebResponse http://www.google.com 
$x.AllElements.InnerText 

を使用できるかどうかを確認するために誘惑されると思います

$x = Html-ToText (new-object net.webclient).DownloadString("test.html") 

# drop the Lines/Characters/etc fields, just export words 
$x | select-Object Words | Export-Csv out.csv -NoTypeInformation 

:あなたが望むだけのプロパティを選択するselect-objectを使用して、コンテンツを置き換えて削除します。

+0

私は使ってみました:$ x |オブジェクトの選択| Export-Csv out.csv -NoTypeInformationと吐き出される唯一のものは単語であってカウントではありませんでした。なぜそれが両方をしなかったのか分かりません。 –

+0

それは私たちの2人を作る。 '' word1 word2 word3 word4 '| measure-object -Word |選択 - オブジェクトの単語| ConvertTo-Csv -NoTypeInformation' ... – TessellatingHeckler

+0

は、.csvファイルではなくコンソールウィンドウに表示します。私はまだ単語を取得しますが、その下に整数はありません –

0

私はそれを理解しました。私がしたことが追加されました + $ a | Measureオブジェクト-Wordをスクリプト内の#html変数の後に置き換えて実行した後、 Html-ToText(新規オブジェクトnet.webclient).DownloadString( "test.html")+ select-Object Words | Export-Csv out.csv -NoTypeInformationと単語数をエクスポートしました - josh s 1分前

関連する問題