PowerShell/Html - 问题 [英] PowerShell / Html - questions
问题描述
我是 Powershell 的新手,我对 html 很陌生.
有一个带有表格的页面,每个单元格都有一个ahref链接,链接的值是动态的,但是我想自动点击的链接总是在第一个单元格中.
我知道在 html/JS 中有 cellindex,在 PS 中可以用吗?
例如,假设我在网站上有这张桌子.
<tr><td><a href="http://example1.com"><div style="height:100%;width:100%">你好世界1</a></td></tr><tr><td><a href="http://example2.com"><div style="height:100%;width:100%">你好世界2
</a></td></tr><tr><td><a href="http://example3.com"><div style="height:100%;width:100%">你好世界3
</a></td></tr>
我想让 powershell 总是点击第一个链接,虽然里面的链接是动态的.
有什么想法吗?提示?
Invoke-WebRequest
的结果返回一个名为 Links
的属性,它是所有超链接的集合在网页上.
例如:
$Web = Invoke-webrequest -Uri 'http://wragg.io' $Web.Links |选择内文,href
返回:
innerText href----- ----马克·拉格 http://wragg.io推特 https://twitter.com/markwraggGithub https://github.com/markwragg领英 https://uk.linkedin.com/in/mwragg
如果您要捕获的链接始终是此列表中的第一个,您可以通过以下方式获取:
$Web.Links[0].href
如果是第二个 [1]、第三个 [2] 等等
我认为没有cellindex"的等价物,尽管有一个名为 AllElements
的属性,您可以通过数组索引访问它.例如,如果您想要页面上的第二个元素,您可以这样做:
$Web.AllElements[2]
如果您需要访问页面中的特定表格,然后访问该表格内的链接,您可能需要遍历 AllElements
属性,直到到达您想要的表格.例如,如果您知道链接位于页面的第三个表格中:
$Links = @()$TableCount = 0$Web.AllElements |ForEach-Object {如果 ($_.tagname -eq 'table'){ $TableCount++ }如果 ($TableCount -eq 3){如果 ($_.tagname -eq 'a') {$链接 += $_}}}$链接 |选择-第一个 1
I am new to Powershell, and I suck at html.
There's a page with a table, and each cell has a ahref link, the value of the link is dynamic, but the link which I want to automate-clicking is always in the first cell.
I know there's cellindex in html/JS, is it usable in PS?
For example, let's say I have this table on a website.
<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>
And I want to make powershell to always click on the first link, the link inside is dynamic though.
Any ideas? Hints?
The result of Invoke-WebRequest
returns a property named Links
that is a collection of all the hyperlinks on a web page.
For example:
$Web = Invoke-webrequest -Uri 'http://wragg.io' $Web.Links | Select innertext,href
Returns:
innerText href
--------- ----
Mark Wragg http://wragg.io
Twitter https://twitter.com/markwragg
Github https://github.com/markwragg
LinkedIn https://uk.linkedin.com/in/mwragg
If the link you want to capture is always the first in this list you could get it by doing:
$Web.Links[0].href
If it's the second [1], third [2] etc. etc.
I don't think there is an equivalent of "cellindex", although there is a property named AllElements
that you can access via an array index. E.g if you wanted the second element on the page you could for example do:
$Web.AllElements[2]
If you need to get to a specific table in the page and then access links inside of that table you'd probably need to iterate through the AllElements
property until you reached the table you wanted. For example if you know the links were in the third table on the page:
$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
这篇关于PowerShell/Html - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!