使用excel vba更改网页上Select标签的内容(从网页下载CSV文件)

时间:2014-01-17 13:25:47

标签: excel vba webpage html-select

我正在尝试使用excel vba

从特定网站下载所有csv文件

以下是我准备的代码:

Sub Gettable()

Dim URL As String

Dim ie As Object
Dim ieDoc As Object
Dim sel_day As Variant

URL = "http://www.bseindia.com/markets/equity/EQReports/BhavCopyDebt.aspx?expandable=3"


Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate URL


Do Until (ie.readyState = 4 And Not ie.Busy)
    DoEvents
Loop

Set ieDoc = ie.document

 '============================================================================   
    ieDoc.getElementsByTagName("Select")("fdate1").Options("02").SelectIndex
 '============================================================================

'ie.Quit
'Set ie = Nothing
'Set ieDoc = Nothing


End Sub

现在我面临的问题是我无法更改内容下拉框(以形成日期)。 我已经尝试过stackoverflow以及其他网站的许多解决方案,但没有取得任何成功。我有很好的编程知识,但一整天都陷入困境。任何帮助,将不胜感激。提前谢谢:)

我最终想要的是下载所有csv文件。我在下载csv文件的同时找到了另一种解决方案,但如果有人给出了我上面发布的这个问题的解决方案,我仍然会感激... :)

我的替代解决方案如下:

Sub try10() 'Took me 10 tries by the way :)

Dim NoOfDays As Long, i As Long
Dim MyDate As Variant


'Since the minimum date can't be less #1/1/2007# so lets loop until Mydate reaches #1/1/2007#.

NoOfDays = Date - #1/1/2007#

For i = 0 To NoOfDays
MyDate = Format((Date - i), "ddmmyy")


Dim myURL As String
myURL = "http://www.bseindia.com/download/BhavCopy/Equity/eq" & MyDate & "_csv.zip"
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send

myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.ResponseBody
    oStream.SaveToFile ("C:\Users\X\Desktop\BhavCopies\eq" & MyDate & "_csv.zip")
    oStream.Close
End If

Next

End Sub

然而,这个解决方案为csv生成一个211kb伪文件,该文件不存在,可以手动处理。 :);)

1 个答案:

答案 0 :(得分:1)

您要控制的项目位于iframe中。要访问iframe中的HTML,请从标记中提取src属性,然后导航到该src和基本URL形成的URL。在这种情况下,src是" Equitydebcopy.aspx",所以如果你导航到" http://www.bseindia.com/markets/equity/EQReports/Equitydebcopy.aspx"您会发现以下行可以让您获得所需的信息。

ie.document.getElementByID("fdate1").Value = "15"
ie.document.getElementByID("fmonth1").Value = "1"
ie.document.getElementByID("fyear1").Value = "2014"
ie.document.getElementByID("btnSubmit").Click