在一些受人尊敬的成员的帮助下,我达到了可以在代码下运行的程度。但碰巧这个脚本在中间耗尽。 我得到的错误终于被粘贴了。
立即:我重新开始我的系统次数只是为了确保它不是一些系统问题但没有运气。
请注意我在代码中添加了以下三行,再次确保它是因为某些IE的原因。
**# try few things
$result = $null
$ie.quit
get-process iexplore | stop-process**
代码在这里开始:
$controls = Get-Content ("d:\users\yarora\desktop\hkd1000.txt")
Add-Type -AssemblyName 'System.Web'
Function getStringMatch
{
Foreach ($control In $controls)
{
$ie = New-Object -COMObject InternetExplorer.Application
#for testing purpose
$control
#encode the special characters in URL like %,& etc.
$controlUri = [System.Web.HttpUtility]::UrlEncode($control)
$site = $ie.Navigate("https://www.xxxy.com/search/all?name=$controlUri")
#$ie.ReadyState
while ($ie.Busy){ sleep -Milliseconds 100 }
$link = $null
$link = $ie.Document.get_links() | where-object {if ($_.innerText){$_.innerText.contains($control)}}
$link.click()
while ($ie.Busy){ sleep -Milliseconds 100 }
[regex]$regex =
@'
(?s).+?<A href="/generics/.*?">(.*?)</A>
'@
$result = $regex.Matches($ie.Document.body.outerHTML) |
foreach {
[PSCustomObject]@{
Name = $control
Saltname = $_.Groups[1].value
}
}
$result | out-file d:\result.txt -append
# try few things
$result = $null
$ie.quit
get-process iexplore | stop-process
}
}
getStringMatch
错误
New-Object:检索具有CLSID {0002DF01-0000-0000-C000-000000000046}的组件的COM类工厂失败 由于以下错误:800706bf远程过程调用失败并且未执行。 (HRESULT的例外情况: 0x800706BF)。 在D:\ script.ps1:22 char:12 + $ ie = New-Object -COMObject InternetExplorer.Application + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 〜 + CategoryInfo:ResourceUnavailable:(:) [New-Object],COMException + FullyQualifiedErrorId:NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
更新 根据Micky的建议,我将-COMobject置于Foreach循环之外。但是在几个循环后脚本仍然出错:
You cannot call a method on a null-valued expression.
At D:\desktop\hkdforsalt1000.ps1:41 char:9
+ $link.click()
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
答案 0 :(得分:2)
这应该是评论,但我没有足够的评论意见。 基本上是Micky所说的......只创建你的$ ie对象一次并在循环中重用它,如下所示:
Function getStringMatc {
$controls = Get-Content ("d:\users\yarora\desktop\hkdforsalt1000.txt")
$ie = New-Object -COMObject InternetExplorer.Application
Foreach ($control In $controls){
$controlUri = [System.Web.HttpUtility]::UrlEncode($control)
$site = $ie.Navigate("https://www.healthkartplus.com/search/all?name=$controlUri")
#rest of your code here...
}
# more code here...
$ie.quit()
}
注意:$ ie.Quit()是一个函数,因此它需要空括号。
此外,您可以在脚本末尾触发垃圾回收以处置未使用的对象:
[System.GC]::Collect()
答案 1 :(得分:1)
这意味着$link
是$ null。因此,$control
中存储的值存在错误,或者特定产品名称不再可用。您应该在调用其$link
方法之前检查click()
的值,然后执行所有操作,因为它们都基于单击链接的假设。