您好我正在尝试从网页http://www.investing.com/indices/us-spx-500-futures-historical-data导入数据 我想要做的是导航到此网页并从日历中选择不同日期范围的宏。
我不明白如何在日历中设置日期。我可以打开它,但我无法“复制”鼠标点击以选择日期 我到目前为止开发的代码是:
Sub Problem()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.navigate "http://www.investing.com/indices/us-30-historical-data"
'
PauseTime = 4 ' Set duration to wait in seconds.
Start = Timer ' Set start time.
Do ' While Timer < Start + PauseTime
DoEvents ' allow other processes to work (the browser to have time to load the webpage)
Loop Until IE.ReadyState = READYSTATE_COMPLETE Or Timer > Start + PauseTime
IE.Document.getElementByID("datePickerIconWrap").Click
'
End Sub
我查看了此页面的源代码,发现了两个有趣的函数, DatePickerGetDate 和 DatePickerSetDate 我尝试使用
运行这两个脚本IE.Document.parentWindow.execScript "widgetHolCalendar.DatePickerGetDate", "javascript"
IE.Document.parentWindow.execScript "widgetHolCalendar.DatePickerSetDate", "javascript"
代码没有给出错误消息,但没有任何改变,所以我不确定代码是否真的在执行某些操作。如果我已正确理解代码,要设置新日期,我必须使用2个参数调用DatePickerSetDate
DatePickerSetDate(date, shifTo)
其中date是一个包含2个元素的数组,shifTO是一个布尔值。我不知道如何使用vba将Arrays传递给这个脚本。
另外,当我调用函数DatePickerGetDate时,我想获得结果并保存在vba数组中
有人可以帮忙吗?
答案 0 :(得分:3)
这对我有用(从页面完全加载时开始......)
Dim f As String
'set the new dates (you just need to plug the required strings in here...
f = "$('#widgetHolCalendar').DatePickerSetDate(['11/05/2013', '11/12/2013'], true);"
IE.document.parentWindow.execScript f, "jscript"
'trigger the page function which refreshes the table
f = "historical_submit();"
IE.document.parentWindow.execScript f, "jscript"
答案 1 :(得分:1)
以下是关于我的建议的可自定义代码示例:
<!DOCTYPE html>
<html>
<head>
<title>Playing with Data in a Date range</title>
<!-- load the jQuery CSS first. The order is important here. Css first, then jQuery, then ui and at last plugins
remove the ".min" if you are on development environment (this let gets you human readable JS Lib Code) -->
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"></script>
<!-- load the datables plugin -->
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js" type="text/javascript"></script>
<script type="text/javascript">
var oTable;
function applyChangesAction(sender)
{
// TODO: Retrieve the data from the server and render it into the table
// they are many ways to do it with datatables. Here is a good
// discussion about it: http://datatables.net/forums/discussion/comment/26734
// after doing that you should be able to refresh the whole table by simply calling this below:
//oTable.fnReloadAjax();
}
$(document).ready(function()
{
// >>> BEGIN DATEPICKER LOGIC <<<
var iOneDayInMs = 60 * 1000 * 60 * 24;
$("#dtPickTo").datepicker(
{
"setDate": new Date(),
"maxDate": new Date()
}
);
$("#dtPickFrom").datepicker(
{
"maxDate": new Date($("#dtPickTo").datepicker("getDate").getTime() - iOneDayInMs)
}
);
// >>> END DATEPICKER LOGIC <<<
// >> BEGIN DATATABLES LOGIC <<
oTable = $("#dataTabPrices").dataTable(
{
"bSortClasses": false,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "http://path/to/your/json/return_script.php",
"fnServerData": function(sSource, aoData, fnCallback)
{
// put your filter criteria data in this object below to get the requested results:
var oaData = { "dtPickFrom": $("#dtPickFrom"),
"dtPickTo": $("#dtPickTo")
};
$.ajax(
{
"dataType": "json",
"type": "POST",
"url": sSource,
"data": JSON.stringify(aoData), // your data filtering criteria for the server side script to return only requested data
"success": fnCallback // closure callback if all data is received and ready to be rendered into the table (refresh logic for button click)
}
);
}
}
);
// >> END DATATABLES LOGIC <<
}
);
</script>
</head>
<body>
<p>
<label for="dtPickFrom">From</label><input type="text" id="dtPickFrom" name="dtPickFrom" /><br />
<label for="dtPickTo">Until</label><input type="text" id="dtPickTo" name="dtPickTo" /> <br />
<input type="button" id="btnApplyChanges" name="btnApplyChanges" value="Apply" onClick="applyChangesAction(this);" />
</p>
<hr />
<table id="dataTabPrices">
<thead>
<tr>
<th>Date</th>
<th>Last</th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Change %</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
尚未完成!这只是你应该如何做的一个基本的例子。阅读我的评论,看看到底有什么遗漏,让它完全正常工作。我的代码经过测试,它的工作原理是它可以工作到目前为止......
我希望这有帮助!