Share via


Retrieve value from HTML tags

Question

Sunday, April 8, 2018 2:50 PM

Hi All,

How can i get the value from the HTML tag, 

$uri = "URL"
$data = Invoke-WebRequest $uri

$table = ($data.ParsedHtml.getElementsByTagName("table") |Where {$_.className -eq "ms-rteTable-3"})

[String] $tdvalues=$table.innerHTML 

$text = $tdvalues
$text -split "<.?tr>" | foreach {write-host $_} 

<tr class="ms-rteTableEvenRow-3"><th class="ms-rteTableFirstCol-3" style="width: 146px;" rowspan="1" colspan="1">​Uzbekistan</th>
<td class="ms-rteTableOddCol-3" style="width: 138px;" rowspan="1">In Force (7-7-2017)</td>
<td class="ms-rteTableEvenCol-3" style="width: 152px;" rowspan="1">​<a href="/xyz.pdf" target="_blank">Model 1</a></td>
<td class="ms-rteTableOddCol-3" style="width: 124px;" rowspan="1">​</td>
<td class="ms-rteTableEvenCol-3" style="width: 80px;" rowspan="1">​6-30-2014</td>

expected Output 

Uzbekistan In Force (7-7-2017) /xuz.pdf Model 1 ' '   6-30-2014

All replies (11)

Monday, April 9, 2018 6:13 AM ✅Answered

Here is how to address the cells contents I  this kind of table:

$table.Rows[0].Cells[0].InnerText

Just walk the rows and cells with an index until you finish the table.

The table will requires some care as you cannot discover the row or cell count.

\(ツ)_/


Wednesday, April 11, 2018 4:14 PM ✅Answered

$table.Rows[$i].Cells[1].InnerText

\(ツ)_/


Wednesday, April 11, 2018 4:24 PM ✅Answered

This gets all of the links in the third column.

for($i=0;$i -lt $table.Rows.Length;$i++){$table.Rows[$i].Cells[2].ChildNodes[0].href}

\(ツ)_/


Sunday, April 8, 2018 2:57 PM

What is it that is not happening?  When you run this code you will get ALL tables and not just one.

The class name in your filter is on the rows and not on the table so this won't work.

$tables = $data.ParsedHtml.getElementsByTagName('table')

Now enumerate the tables collection to find the table.  It would be best if the table had a name or ID.

\(ツ)_/


Sunday, April 8, 2018 3:08 PM

What do i need to change in my code. Table is getting filtered by table class name


Sunday, April 8, 2018 4:04 PM

If the code is returning the correct table then just get the tows and cells then the innerText of each cell.

Can you post the URL so I can give you an example?

\(ツ)_/


Monday, April 9, 2018 3:34 AM

Here is the URL

https://www.treasury.gov/resource-center/tax-policy/treaties/Pages/FATCA.aspx


Monday, April 9, 2018 7:00 AM

Thanks a lot for the help, is there a easyway to count no of rows.


Monday, April 9, 2018 7:13 AM

no.  You just have to increment until there are no more rows.

\(ツ)_/


Wednesday, April 11, 2018 4:03 PM

Thanks . I got the rows $Trow=($table.rows.length())-1;

How can i retrieve the URL's from cell 2


Thursday, April 12, 2018 4:48 PM

Wonderfull. thanks a lot for all the help