2017-03-27 2 views
1

私はPowershellを初めて使い、htmlで吸う。PowerShell/Html - 質問

テーブルがあるページがあり、各セルにはahrefリンクがあり、リンクの値は動的ですが、自動化したいリンクは常に最初のセルにあります。

html/JSにcellindexがあることは知っていますが、PSで使用できるのですか?

たとえば、私はこのテーブルをウェブサイトに持っているとしましょう。

<table> 

<tr> 
    <td> 
    <a href="http://example1.com"> 
    <div style="height:100%;width:100%"> 
     hello world1 
    </div> 
    </a> 
</td> 

</tr> 

<tr> 
    <td> 
    <a href="http://example2.com"> 
    <div style="height:100%;width:100%"> 
     hello world2 
    </div> 
    </a> 
</td> 
</tr> 

<tr> 
    <td> 
    <a href="http://example3.com"> 
    <div style="height:100%;width:100%"> 
     hello world3 
    </div> 
    </a> 
</td> 
</tr> 

</table> 

そして、私はいつも最初のリンクをクリックするようにpowershellを作っていますが、内部のリンクは動的です。

アイデア?ヒント?

答えて

3

Invoke-WebRequestの結果は、Webページ上のすべてのハイパーリンクのコレクションであるLinksという名前のプロパティを返します。例えば

$Web = Invoke-webrequest -Uri 'http://wragg.io' $Web.Links | Select innertext,href 

戻り値:

innerText     href 
---------     ---- 
Mark Wragg     http://wragg.io 
Twitter      https://twitter.com/markwragg 
Github      https://github.com/markwragg 
LinkedIn     https://uk.linkedin.com/in/mwragg 

キャプチャするリンクは、あなたがやってそれを得ることができ、常にこのリストの最初のものである場合:

$Web.Links[0].href 

2番目の[1]、3番目の[2]などであれば

"cellindex"に相当するとは思われませんが、配列インデックスを介してアクセスできるAllElementsという名前のプロパティがあります。例えば、あなたが例えばできたページの2番目の要素を望んでいた場合の対処:あなたはページ内の特定のテーブルに取得する必要があり、その後、そのテーブルの内部リンクにアクセスした場合

$Web.AllElements[2] 

あなたはおそらく反復処理する必要があるだろうAllElementsプロパティを使用して、必要なテーブルに到達するまで実行します。たとえば、あなたがリンクがページ上の3番目のテーブルにいた知っている場合:

$Links = @() 
$TableCount = 0 

$Web.AllElements | ForEach-Object { 

    If ($_.tagname -eq 'table'){ $TableCount++ } 

    If ($TableCount -eq 3){ 

     If ($_.tagname -eq 'a') { 
      $Links += $_ 
     } 
    } 
} 

$Links | Select -First 1 
+0

私はcellindexを使用することはできますか?選択したいタグの上に他のタグ「a」がある場合はどうなりますか? –

+0

おそらく最も単純な解決策である「リンク」プロパティを忘れてしまったので、私は私の答えを見直しました。おそらくインデックスを持つAllElementsプロパティを使用しているのと同じ意味でない限り、cellindexと同等のものはないと思います。 'AllElements'の各項目は' innerhtml'と 'outerhtml'プロパティを持っていますので、' outerhtml'にアクセスすることで 'a'タグの周りにタグを取得できると思います。お役に立てれば。 –

+0

ありがとうMark、もう1つの一般的な質問:特定のhtlm要素のインデックス番号がわかっているサードパーティのツールかPSメソッドがありますか? –

0

[OK]を、起動-WebRequestクラスのメソッドは、マークのリンクではなく、私のページに取り組んでいます。私は次のように気づい

:;

<table id="row" class="simple"> 
<thead> 
<tr> 
<th></th> 
<th class="centerjustify">File Name</th> 
<th class="centerjustify">File ID</th> 
<th class="datetime">Creation Date</th> 
<th class="datetime">Upload Date</th> 
<th class="centerjustify">Processing Status</th> 
<th class="centerjustify">Exceptions</th> 
<th class="centerjustify">Unprocessed Count</th> 
<th class="centerjustify">Discarded Count</th> 
<th class="centerjustify">Rejected Count</th> 
<th class="centerjustify">Void Count</th> 
<th class="centerjustify">PO Total Count</th> 
<th class="centerjustify">PO Total Amount</th> 
<th class="centerjustify">CM Total Count</th> 
<th class="centerjustify">CM Total Amount</th> 
<th class="centerjustify">PO Processed Count</th> 
<th class="centerjustify">PO Processed Amount</th> 
<th class="centerjustify">CM Processed Count</th> 
<th class="centerjustify">CM Processed Amount</th> 
<th class="centerjustify">Counts At Upload</th></tr></thead> 
<tbody> 
<tr class="odd"> 
<td><input type="radio" disabled="disabled" name="checkedValue" value="12047" /></td> 
<td class="leftjustify textColorBlack"> 
<a href="loadConfirmationDetails.htm?fId=12047">52017_52017_20170327_01.txt</a></td> 
<td class="centerjustify textColorBlack">1</td> 
<td class="datetime textColorBlack">Mar 27, 2017 0:00</td> 
<td class="datetime textColorBlack">Mar 27, 2017 10:33:24 PM +03:00</td> 
<td class="centerjustify textColorBlack"> 

「?loadConfirmationDetails.htm FID = 12047」でFID部分が動的であるが、私は使用することができるかもしれパターンに気づいそれは次のページの最後の部分です。例えば

: - ; Webページを呼び出す以外の自動コピーこれで、行 『" https://aaa.xxxxxxx.com/aaa/community/loadConfirmationDetails.htm?fId= 12047

とテーブルのIDと呼ばれる、独特である』私は完全に別の方法を使用することができるのだろうかそのソースHTMLからID情報とメインリンクでそれを連結する?

私はそれを超えてアイデアを本当にしています。