Vbscript由于某些原因变得非常慢

Vbscript由于某些原因变得非常慢,vbscript,Vbscript,因此,我试图使用一个自动internet explorer从adwords中获取一些数据,脚本工作正常,但过了一段时间,它就慢下来了。你能理解为什么会发生这种情况,或者有什么方法可以避免这种情况吗 'code to halt internet explorer Sub WaitForLoad(obj) Do While ie.Busy: Loop Do While obj.readyState <> 4: Loop wscript.sleep(100) En

因此,我试图使用一个自动internet explorer从adwords中获取一些数据,脚本工作正常,但过了一段时间,它就慢下来了。你能理解为什么会发生这种情况,或者有什么方法可以避免这种情况吗

'code to halt internet explorer
Sub WaitForLoad(obj)
    Do While ie.Busy: Loop
    Do While obj.readyState <> 4:  Loop
    wscript.sleep(100)
End Sub

'code to save the HTML
Sub SaveHTML(savePath, fileName)
    Dim objFileSystem, objOutputFile
    Dim strOutputFile

    ' generate a filename base on the script name
    strOutputFile = savePath & fileName & ".html"

    Set objFileSystem = CreateObject("Scripting.fileSystemObject")
    Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile,2, TRUE)



    objOutputFile.WriteLine(ie.document.getElementById("btfResults").innerHTML)

    objOutputFile.Close

    Set objFileSystem = Nothing
End Sub


'create ie object
Set ie = WScript.CreateObject("InternetExplorer.Application")

'set the ie properties
ie.ToolBar = 0
ie.StatusBar = 1
ie.Width = 999
ie.Height = 999
ie.Left = 0
ie.Top = 0
ie.Visible = 1




  ie.Navigate "https://adwords.google.co.uk/um/Logout"
  WaitForLoad(ie)

dim objFSO,objFile,strContents,objReadFile,WriteStuff
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("logon.log")

Set objReadFile = objFSO.OpenTextFile("logon.log", 1)
    strContents = objReadFile.ReadAll
    'Wscript.Echo strContents +1 
    objReadFile.Close



  ie.Document.getElementByID("Email").Value = "GMAILADDRESS"

  ie.Document.getElementByID("Passwd").Value = "PASSWORD"


  'Note: You could just get the form and submit it, but
  'you'll miss out on any special JavaScript associated 
  'with the Submit button.
  ie.Document.getElementByID("signIn").Click
WaitForLoad(ie)
  ie.Navigate "https://adwords.google.co.uk/o/Targeting/Explorer?"
WaitForLoad(ie)

 dim cn, rs,rows

set cn = CreateObject("ADODB.Connection")
set rs = CreateObject("ADODB.Recordset")
cn.connectionstring = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test;Uid=root;"
cn.open
WaitForLoad(ie)
  ie.Document.getElementsByTagName("input")(3).click
dim j
j=strContents
dim document_start
document_start=ie.Document
Do While true
rs = cn.execute("SELECT * FROM `emails1` WHERE `send`>'"+CStr(j)+"' order by `send` asc limit 0,1")
j=rs(1)
ie.Document.getElementsByTagName("textarea")(0).value=CStr(rs(0))
ie.Document.getElementsByTagName("button")(0).click

WScript.Sleep 4000
dim value,value1,value2,str,str1,i,ssf,ssf1,rep,SECOND,dpth
SECOND=true
dpth=0



Do while (SECOND and dpth<3)
SECOND= false
i=0
'Set WriteStuff = objFSO.OpenTextFile("tst.log", 2, True)
for each value in ie.document.getElementsByTagName("tr")
if (i>14  and  i<>17 and i<>18 and i<>19 and i<60 ) then str=str+value.innertext+ "__________________"
i=i+1
next
str = replace(replace(replace(Trim(Replace(Replace(str, vbCrLf, "/"), vbTab, "")),"////// ",""),"/////","////"),"////Keyword///Competition////Global Monthly Searches ////Local Monthly Searches __________________","")
ssf=split(str,"__________________")
for each rep in ssf
if (StrComp(Cstr(rep),"")<>0) then
ssf1=split(rep,"////")
cn.execute("INSERT IGNORE INTO `test`.`emails1` (`word`,`competition`,`GlobalMonthly`,`LocalMonlthly`)VALUES ('"+Cstr(ssf1(0))+"','"+Cstr(ssf1(1))+"','"+Cstr(ssf1(2))+"','"+Cstr(ssf1(3))+"');")
on error resume next
'WriteStuff.WriteLine(Cstr(ssf1(2)) & vbCrLf)
end if
next

for each value in ie.document.all
if (StrComp(Cstr(value.className),"sJQB")=0) then 
value.click
dpth=dpth+1
WScript.Sleep 500
SECOND=true
Exit For
end if
next


'WriteStuff.Close
loop


Set WriteStuff = objFSO.OpenTextFile("logon.log", 2, True)
j=j+1
WriteStuff.WriteLine(j)

WriteStuff.Close
ie.document=document_start
loop


ie.quit
“停止internet explorer的代码
次级WaitForLoad(obj)
忙的时候做
Do While obj.readyState 4:循环
wscript.sleep(100)
端接头
'保存HTML的代码
子保存HTML(保存路径、文件名)
Dim objFileSystem,objOutputFile
Dim strOutputFile
'根据脚本名称生成文件名
strOutputFile=savePath&fileName&“.html”
设置objFileSystem=CreateObject(“Scripting.fileSystemObject”)
设置objOutputFile=objFileSystem.CreateTextFile(strOutputFile,2,TRUE)
objOutputFile.WriteLine(即.document.getElementById(“btfResults”).innerHTML)
objOutputFile.Close
设置objFileSystem=Nothing
端接头
'创建ie对象
设置ie=WScript.CreateObject(“InternetExplorer.Application”)
'设置ie属性
ie.ToolBar=0
ie.StatusBar=1
即宽度=999
即高度=999
即左=0
即Top=0
即可见=1
即“导航”https://adwords.google.co.uk/um/Logout"
等待加载(ie)
dim objFSO、objFile、strContent、objReadFile、WriteStuff
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objFile=objFSO.GetFile(“logon.log”)
设置objReadFile=objFSO.OpenTextFile(“logon.log”,1)
strContents=objReadFile.ReadAll
'Wscript.Echo strContents+1
objReadFile.Close
ie.Document.getElementByID(“电子邮件”).Value=“Gmail地址”
ie.Document.getElementByID(“Passwd”).Value=“PASSWORD”
注意:你可以拿到表格并提交,但是
您将错过任何与之相关的特殊JavaScript
'使用提交按钮。
ie.Document.getElementByID(“登录”)。单击
等待加载(ie)
即“导航”https://adwords.google.co.uk/o/Targeting/Explorer?"
等待加载(ie)
尺寸cn、rs、行
set cn=CreateObject(“ADODB.Connection”)
set rs=CreateObject(“ADODB.Recordset”)
cn.connectionstring=“Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test;Uid=root;”
中国公开
等待加载(ie)
ie.Document.getElementsByTagName(“输入”)(3)。单击
dim j
j=结构元素
dim文件\u开始
document\u start=ie.document
做正确的事
rs=cn.execute(“从`emails1`中选择*,其中`send`>'”+CStr(j)+“按`send`asc limit 0,1排序”)
j=卢比(1)
ie.Document.getElementsByTagName(“textarea”)(0).value=CStr(rs(0))
ie.Document.getElementsByTagName(“按钮”)(0)。单击
WScript.Sleep 4000
尺寸值,值1,值2,str,str1,i,ssf,ssf1,rep,SECOND,dpth
秒=真
dpth=0

第二个好的开始是:清理代码(缩进),使用option explicit,使变量名易于理解,在不需要时关闭连接,通过生成子函数或函数进行重构,在不需要时将对象引用设置为Nothing。然后,您会看到,在写入日志的每一行中,您都会一次又一次地打开同一个文本文件,并随着每次li的增加而逐渐降低速度ne.打开一次文本文件,在运行过程中写下你的东西,准备好后关闭文本文件,这将是加快代码速度的一个良好开端。@AutomatedChaos本应作为答案发布。我完全同意。我这样做是为了跟踪我停下来的地方。谢谢,不过我会看看我是否能做些什么。所以是的a、 我将对象设置为null,仍然是空的。这就好像文档不断变大或什么的。