2017-01-11 40 views
0

私はMS Powershellを使用して仕事を自動化したいと思います。そのウェブサイトの下の私のコードを見てください。このコードは正常に動作しています。Powershellを使用してhtmlから特定のデータを取得する

$username = "usern" 
$password = "pass" 
$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true 
$ie.navigate("http://www.exemple.com") 
while($ie.ReadyState -ne 4) {start-sleep -m 100} 
$ie.document.IHTMLDocument3_getElementByID("textfield").value = $username 
$ie.document.IHTMLDocument3_getElementByID("textfield2").value = $password 
$ie.document.IHTMLDocument3_getElementByID("btnLogin").Click(); 

ここで、レポートをダウンロードするには、HTML本文から数字を抽出して変数に挿入する必要があります。私がそれをやっている理由は、ページにアクセスするたびにこの番号が変わるからです。次の画像を参照してください。番号はWebページのHTML本文の中にあります。常に12桁です:

image(click here) これは私の問題です。私は変数の中でこの番号を得ることができません。もしできれば、Powershellコードを以下のスクリプトで完成させます。あなたが見る

$output = "C:\Users\AlexSnake\Desktop\WeeklyReport\ReportName.pdf" 
Invoke-WebRequest -Uri http://www.exemple.com.br/pdf_pub/xxxxxxxxxxxx.pdf -OutFile $output 

は「XXX ..」私は変数のために交換し、あなたのコード 中のこのビットの後レポート

答えて

1

をダウンロードします($ ie.ReadyState -ne 4){開始、睡眠100}

はこれを試してみてください-m:これは動作するはず

$($ie.Document.getElementsByTagName("a")).href | ForEach { 
    # The next line isn't necessary, but just to demonstrate iterating through all the anchor tags in the page (feel free to comment it out) 

    Write-Host "This is the href tag that I'm enumerating through: $_" 

    # And this bit checks for that number you're looking for and returns it: 
    if($_ -match "javascript:openwindow('/\.\./\.\./[\d+]\.pdf'.*)") 
    { 
     $matches[1] 
    } 
} 

+0

少なくとも括弧はバックスラッシュでエスケープする必要があり、括弧でグループを挿入する必要がある唯一の数ではなく、クラスを取得します。 'もし($ _ -match "ジャバスクリプト:。openwindowの\( '/\.\./\.\./ pdf_pub /(\ dは+)\ PDF' * \)")' – LotPings

+0

@kuraara、私を助けることができます結果をコンソールに出力する方法は?私は変数 '$ matches [1]'をコンソールに表示して数字と一致するかどうかを調べるにはどうすればいいですか? – AlexSnake

0

私の質問の答えは以下のコードを参照してください。

$($ie.Document.getElementsByTagName("a")).href | ForEach { 

if($_ -match '(\d+)\.pdf') 
{ 
    $matches[1] 
    } 
} 

ありがとうございます!

関連する問題