Excel VBA HTML检索

问题描述:

我有一个功能齐全的宏,它通过人员记录列表,如果他们是离开人员或从未开始工作。唯一的问题是它的一个非常缓慢的过程,搜索(〜10000个字符)所有的HTML代码创建的字符串时Excel VBA HTML检索

我想知道是否有限制检索是网页的只是一部分的方式

我目前使用的宏下,经过各行这个宏迭代和长10000个字符从URL中的每个人的人员页

Sub RetrieveEndDate() 
Dim myArray() As Variant, Search As Variant 
Dim strURL As String, strCSV As String, dbClose As String 


Application.ScreenUpdating = False 
Application.Calculation = xlCalculationManual 


Call LogOn 


RowsWithData = Application.CountA(Range("A:A")) 

For R = 2 To RowsWithData 

Application.StatusBar = R & " Out of " & RowsWithData 


UKNo = Cells(R, 1).Value 
    strURL = "http://www.pers.fs.com/People_Detail.asp?Pers_no=" & UKNo &  "&mode=CURRENT" 

Set http = CreateObject("MSXML2.XMLHTTP") 
    http.Open "GET", strURL, False 
    http.Send 
    strCSV = http.responseText 

    Cells(R, 3).Value = strCSV 
'Works of if employee has left, never started or if neither of them leaves blank 
    If InStr(1, strCSV, "Employee has Left") > 0 Then 
     Cells(R, 2).Value = "Left" 

    ElseIf InStr(1, strCSV, "Non-Starter") > 0 Then 
     Cells(R, 2).Value = "Did not start" 

    Else 
     Cells(R, 2).Value = "" 

    End If 


Set http = Nothing 

Next R 

1 

Application.ScreenUpdating = True 
Application.Calculation = xlCalculationAutomatic 

End Sub 

从网页中获取是〜代码拉,但信息我感兴趣的是在页面的开始部分,如下面的“(员工已经离开)”,这是从底线第三

<head> 

<title> List</title> 

<link rel="stylesheet" href="_stylesheets/atc.css" type="text/css"> 

</head> 



<body CLASS="Skill" > 

<form name="People_Detail" method="Post" action=History_list.asp> 

<P><INPUT id="Pers_No" type = "HIDDEN" name="Pers_No" value=UK111111 ></P> 

<P><INPUT id="mode" type = "HIDDEN" name="mode"Value="HISTORY_LIST"></P> 









    <Table Border = 0 CellPadding = 0 width = 100% > 



<TR><TR><TD Colspan = 2 ><H1 id=Test name=test>Current Active Record<BR>(Employee has Left)</H1><TD align = right> 



    <P><INPUT id="btnSubmit" name="btnSubmit" type="SUBMIT" value="View Record History List"></P> 

    </TD></TD></TR></TR> 

AFAIK有没有办法用XMLHTTP做到这一点。

This KB article包含使用WinInet API执行下载的代码。

While bDoLoop循环读取Len(sReadBuffer)块中的URL,您可以修改它以添加一个条件并随时退出循环。

如果您想要以特定的偏移量开始下载(并且服务器支持它),则还可以尝试InternetSetFilePointer

我有一个类似的问题。在某个网站上的响应文本非常大,以至于我一直在寻找它的宏。我提出的解决方案如下。首先,我在响应文本上使用了SPLIT功能。

arr_1 = Split(my_var, "zc-st-a", -1, vbTextCompare) 

您没有提供足够的源代码,我要具体,但通常有一些标签,你可以在那一刹那打破了响应文本分解成你想要的数据数组元素,并没有这些元素有用的信息。接着使用过滤功能在arr_1过滤掉无用的元素

arr_2 = Filter(arr_1, "zc-pg-y", True, vbTextCompare) 

最后,你可以结合使用JOIN功能存在于arr_2有用的元素。

​​

以我的情况下,使用这种方法,使响应文本较小的还原我的宏运行时间为1小时15分钟到15分钟。希望这可以帮助