我是 Powershell 的新手,只是将它用于个人用途。我一直在尝试从网站中提取特定信息以包含在家庭电子邮件中。通过阅读论坛,我使用Invoke-WebRequest
cmdlet非常好,但很快就遇到了无法访问页面加载时动态构建的内容的限制。
感谢这些论坛,我随后发现了IE对象以及如何提取数据。我有一个网站的运气,但我试过的另一个网站不一样。希望得到一些帮助来解决它。
以下是页面检查代码的片段,其中我的兴趣目标已突出显示。
下面是我尝试提取该文本字符串的代码。我尝试了很多次迭代和方法但没有成功。但奇怪的是,$ie.Document
对象应该有一个“body”对象,但是当我尝试访问它时,我得到一个null对象错误。我注意到Document对象本身有一个getElementsByTagName
方法,所以我试过了。它没有getElementsByClassName
方法。
请注意,我加载的网址是“https”,所以我想知道这是否会导致问题。建议赞赏!如果我可以获得HTML的大概,我可以做一些字符串操作来获得我想要的东西。
# Create IE object and load URL
$WeatherURL = "https://weather.com/weather/today/l/77630"
$ie = New-Object -comobject "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($WeatherURL)
# Wait for the page to load
while ($ie.Busy -eq $true -Or $ie.ReadyState -ne 4) {Start-Sleep 2}
$Doc = $ie.Document
$Weather0 = $Doc.getElementsByTagName('span') `
| ?{$_.getAttribute('class') -eq "today-wx-descrip"} | Select-Object -First 1
答案 0 :(得分:0)
你应该替换
$Weather0 = $Doc.getElementsByTagName('span') `
| ?{$_.getAttribute('class') -eq "today-wx-description"} | Select-Object -First 1
使用
$Weather0 = $Doc.getElementsByTagName('span') `
| ?{$_.getAttribute('class') -eq "today-wx-descrip"} | Select-Object -First 1
注意 today-wx-description vs today-wx-descrip 。