在一些受人尊敬的成员的帮助下,我达到了可以运行以下代码的地步。但碰巧这个脚本在中间耗尽了。我得到的错误终于被粘贴上了.
到现在为止:我重新启动我的系统次数,只是为了确保它不是什么系统问题,但没有运气。
请注意,我在代码中添加了以下三行代码,以确保这是因为某些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误差
新对象:使用CLSID {0002DF01-0000-C000-000000000046}检索组件的COM类工厂,由于以下错误而失败: 800706bf远程过程调用失败且未执行。(HRESULT除外: 0x800706BF)。At D:\script.ps1:22 char:12 + $ie =新对象-COMObject InternetExplorer.Application +~+ CategoryInfo : ResourceUnavailable:(:) New-Object,COMException + FullyQualifiedErrorId : NoCOMClassIdentified,ResourceUnavailable
根据米奇的建议,更新了,我把-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发布于 2015-01-13 09:44:27
这意味着$link是$null。因此,要么存储在$control中的值出现错误,要么特定的产品名称不再可用。在调用$link方法并在此之后执行所有操作之前,您应该检查它的值,因为它们都基于单击链接的假设。
发布于 2015-01-13 02:04:20
这可能是一个评论,但我没有足够的观点来评论。基本上是米奇说的,…只创建一次$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()https://stackoverflow.com/questions/27908210
复制相似问题