IE在VBA代码中失去连接 [英] IE in vba code lose connection

查看:51
本文介绍了IE在VBA代码中失去连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在www.soccer24.com网站上制作了一个报废的宏,我想在主页上获取每场比赛的所有详细信息.

I made a macro that scrap in formation from the website www.soccer24.com I want to get all the details of every match in the home page.

我已经编写了代码,并且可以运行,但是当宏运行时,突然之间,IE连接丢失了.我的意思是,该宏必须进入同一页面内的150个不同的网站,如下所示:

I already made the code and it works but when the macro is running, suddenly, the IE conection is lost. I mean, the macro have to go into 150 different websites inside the same page, like this:

https://www.soccer24.com/match/ Qo8j57nO /#odds-comparison; 1x2-odds;上半部

https://www.soccer24.com/match/Qo8j57nO/#odds-comparison;1x2-odds;1st-half

每个链接中唯一更改的是 bounded

the only thing that changes in each link is the id that is bolded

该宏转到网站,提取一些信息,然后再访问另一个,提取信息,依此类推.

the macro goes to the website, pulls some information, and goes to another, pulls information, and so on.

当宏位于第40页时,IE连接就断开了,它不再起作用了,我无法访问任何网站.当我关闭程序并再次打开它时,我恢复了Internet连接,但是当它进行40或50次迭代时,它始终是相同的,IE连接中断了,并且没有打开任何网站.

when the macro is about the page 40, the IE connection just breaks, and it doesn't work anymore and I can't go to any website. When I close the program and open it again, I recover the internet connection, but it's always the same when it goes on the 40 or 50 iteration, the IE connection breaks and doesn't open any website.

就像IE VBA有一个工作时间限制"或网站访问限制",我不知道发生了什么.

its like IE VBA has a "working time limit" o "websites visit limit" I don't know whats happening.

我正在使用的代码是这样的:

The code That I am using is this one:

代码段1

代码段2

因此它可以完美运行,但最终尝试进入网站时,它会在150的40或50迭代中出现.

so it works perfectly, but eventually in the 40 or 50 iteration of 150. when it tries to go to a website:

IE.Navigate" https://www.soccer24.com/match/"&范围("A"和i).值&"/#odds-comparison; 1x2-odds; 1st-half"

IE.Navigate "https://www.soccer24.com/match/" & Range("A" & i).Value & "/#odds-comparison;1x2-odds;1st-half"

Internet Explorer显示无连接",并且不再起作用.就像IE VBA有一个工作时间限制"或网站访问限制"

the internet explorer shows "no conection" and it doesn't work anymore. it's like IE VBA has a "working time limit" o "websites visit limit"

推荐答案

在网络抓取中,这应该是您最好的朋友:

In webscraping this should be your best friend:

Option Explicit

Public Sub SaveResponse(response As String) 'don't save as txt, but as html to easily see the structure

Dim fso As Object, oFile As Object
Dim filedir As String

filedir = CreateObject("WScript.Shell").specialfolders("Desktop")

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(filedir & "\response.html", Unicode:=True) 'Default is ANSI encoding, change to UNICODE

oFile.WriteLine response
oFile.Close

End Sub

对于IE自动化,其用法如下:

For IE automation it is used like this:

SaveResponse myIE.document.body.innerHTML

它将响应保存为HTML文件在您的桌面上.您将能够看到错误之前最后收到的响应的样子.

It will save response as HTML file on your desktop. You will be able to see how last received response before error looked like.

IE自动化有很多问题,但是我希望的最后一件事是完全失去Internet连接.这需要更仔细地检查.Chrome会在IE无法同时打开此网站或任何其他网站吗?

There are multiple things which can go wrong with IE automation, however the last thing I would expect is entirely losing internet connection. This needs to be more closely checked. Will Chrome open this or any other website at the same time IE can't?

要克服某些IE问题,您可能需要每X个请求 IE.Quit .另外,在后台删除旧的IE实例有时会很有用,特别是对于处理内存泄漏:

To overcome some IE issues, you may want to IE.Quit every X number of requests. Additionally removing old IE instances in background is sometimes helpful, especially to handle memory leak:

Public Sub TerminateIE()

Dim objWMIService As Object
Dim colItems, objItem

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

On Error GoTo ErrHandler
For Each objItem In colItems
    If objItem.Name = "iexplore.exe" Then objItem.Terminate
Next objItem

Exit Sub
ErrHandler:
MsgBox ("error " & Err.Name & "with description " & Err.Description & String(2, vbLf) & "Nothing object test for objItem returns: " & objItem Is Nothing)
End Sub

这篇关于IE在VBA代码中失去连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆