在脚本中使用 GetElementsByClassName [英] Use GetElementsByClassName in a script
问题描述
我正在尝试编写一个 PowerShell 脚本,以从网站上获取所有名为newstitle"的类中的文本.
I'm trying to write a PowerShell script to get the text within all the classes named "newstitle" from a website.
这就是我所拥有的:
function check-krpano {
$geturl=Invoke-WebRequest http://krpano.com/news/
$news=$geturl.parsedhtml.body.GetElementsByClassName("newstitle")[0]
Write-Host "$news"
}
check-krpano
它显然需要更多的调整,但到目前为止,它不起作用.
It obviously needs much more tweaking, but so far, it doesn't work.
我设法使用 GetElementById 编写了一个脚本,但我不知道 GetElementsByClassName 的语法,老实说,我没有找到太多关于它的信息.
I managed to write an script using GetElementById, but I don't know the syntax for GetElementsByClassName, and to be honest, I haven't been able to find much information about it.
注意:
我已经勾选了我问题的正确答案,但这不是我选择在脚本中使用的解决方案.
I've ticked the right answer to my question, but that's not the solution that I had chose to use in my script.
虽然我可以使用 2 种方法在包含某个类的标签中找到内容,但它们比搜索链接要慢得多.
Although I was able to find the content within a tag containing a certain class, using 2 methods, they were much slower that searching for links.
这是使用 Measure-Command 的输出:
Here is the output using Measure-Command:
- 使用 parsedhtml.body 搜索包含newstitle"类的 div -> 29.6 秒
- 使用 Allelements 搜索包含类newstitle"的开发者 -> 10.4 秒
- 搜索其元素 'href' 包含 #news 的链接 -> 2.4 秒
所以我将链接方法的答案标记为有用.
So I have marked as useful the Links method answer.
这是我的最终脚本:
function check-krpano {
Clear-Host
$geturl=Invoke-WebRequest http://krpano.com/news
$news = ($geturl.Links |Where href -match '\#news\d+' | where class -NotMatch 'moreinfo+' )
$news.outertext | Select-Object -First 5
}
check-krpano
推荐答案
如果你想知道如何让 GetElementsByClassName 工作,我想知道.我昨天刚遇到这个,时间不够了,所以我想出了一个解决方法:
If you figure out how to get GetElementsByClassName to work, I'd like to know. I just ran into this yesterday and ran out of time so I came up with a workaround:
$geturl.ParsedHtml.body.getElementsByTagName('div') |
Where {$_.getAttributeNode('class').Value -eq 'newstitle'}
这篇关于在脚本中使用 GetElementsByClassName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!