我遇到的问题似乎有一个简单的答案。我想让我的网站完成从网页中抓取一段文本的简单任务。
我基本上希望我的代码看起来像......
var str = TextFromWebPage;
我要“抓取”的文字在http://dasd-sharepoint.dasd.org/Schools/STEMAcademy/Pages/default.aspx
如果您在页面上查看(尝试使用 Control F 并搜索当前日期,例如:)"May-9-2013:"
以获取接下来五个日期的列表。我想获取该列表并在 Javascript 中的变量中使用它。
所以我更喜欢的最终结果是
var str = new Array("May-9-2013: “F” Day", "May-10-2013: “G” Day", "May-13-2013: “H” Day", "May-14-2013: “I” Day", "May-15-2013: “J” Day", "May-16-2013: “K” Day");
目的:基本上为了学校我总是检查时间。因此,我没有做数学运算,而是做了一个快速的小程序,查看当前时间与当前课程结束的时间。然而,在我的学校,我们有不同的“字母日”,这意味着不同日子的课程长度不同。字母天数以 12 天为周期,这意味着。周一到周五可能是AE,但这意味着下一周的周一将是FJ。更不用说是否有下雪天或休息日。因此,与其使用日历,我认为必须有一种方法可以从该地区的网页中获取这些数据,并在代码中使用它来自动检查它是哪一天的字母。
感谢您的任何帮助!
这是我正在做的事情的代码......
<!DOCTYPE html>
<html>
<head>
//<link rel="stylesheet" type="text/css" href="graphics.css">
<script>
//==========| Data |==========
//These array lists contain the times the class periods start, end, and their title
var Ptest = new Array("starts: 07:25 ends: 15:00 title: Testing", "starts: 07:25 ends: 30:00 title: Testing");
var P14 = new Array("starts: 07:25 ends: 09:00 title: Pd: 1", "starts: 09:04 ends: 10:34 title: Pd: 2", "starts: 10:38 ends: 11:08 title: Lunch", "starts: 11:12 ends: 12:42 title: Pd: 3", "starts: 12:46 ends: 14:23 title: Pd: 4");
var P17 = new Array("starts: 07:25 ends: 08:20 title: Pd: 1", "starts: 08:24 ends: 09:14 title: Pd: 2", "starts: 09:18 ends: 10:08 title: Pd: 3", "starts: 10:12 ends: 11:02 title: Pd: 4", "starts: 11:06 ends: 11:36 title: Lunch", "starts: 11:40 ends: 12:30 title: Pd: 5", "starts: 12:34 ends: 13:24 title: Pd: 6", "starts: 13:28 ends: 14:23 title: Pd: 7");
var P57 = new Array("starts: 07:25 ends: 09:00 title: Pd: 5", "starts: 09:04 ends: 10:34 title: Pd: 6", "starts: 10:38 ends: 11:08 title: Lunch", "starts: 11:12 ends: 12:42 title: Pd: 7", "starts: 12:46 ends: 13:36 title: Advisory", "starts: 13:40 ends: 14:23 title: Seminar");
//---------------------------------
//==========| Variables |==========
var input = P17; //What set of data to use
var currentSlot = 0; //What the current Period is
var over = false; //If the current school day if over
//---------------------------------
//==========| Actions |==========
window.onload=function() {
setInterval(function() {refresh();}, 1); //Loop
}
//---------------------------------
//==========| Core Functions |==========
function refresh() {
display();
updateClock();
}
function display() {
var currentTime = getTime();
var timeInSec = ((parseInt(currentTime.slice(0,2)))*3600)+((parseInt(currentTime.slice(3,5)))*60)+(parseInt(currentTime.slice(6,8)));
if (currentSlot >= input.length) {over = true;}
if (over == false) {
var data = input[currentSlot];
var nextSlot = input[currentSlot + 1];
var PdEndTime = ((parseInt(data.slice(20,23)))*3600)+((parseInt(data.slice(23,25)))*60);
var PdStart = data.slice(8,13);
var PdEnd = data.slice(20,25);
var PdTitle = data.slice(32,data.length);
if (nextSlot!==undefined) {
var NPdStart = nextSlot.slice(8,13);
var NPdEnd = nextSlot.slice(20,25);
var NPdTitle = nextSlot.slice(32,nextSlot.length);
}
var timeLeft = PdEndTime - timeInSec;
var hLeft = Math.floor(timeLeft / 3600);
var mLeft = Math.floor(timeLeft / 60);
var sLeft = Math.floor(timeLeft % 60);
if (hLeft > 0) {mLeft = mLeft - (hLeft*60);}
hLeft = add0(hLeft);
mLeft = add0(mLeft);
sLeft = add0(sLeft);
if (timeInSec > PdEndTime) {currentSlot++;}
document.getElementById("display").innerHTML = PdTitle+" ends at "+PdEnd+", in "+hLeft+":"+mLeft+":"+sLeft;
}
else {document.getElementById("display").innerHTML = "School is over!";}
}
//---------------------------------
//==========| Misc Functions |==========
function getTime() {
var clock = new Date();
var h = clock.getHours();
var m = clock.getMinutes();
var s = clock.getSeconds();
h = add0(h);
m = add0(m);
s = add0(s);
return h+":"+m+":"+s;
}
function updateClock() {
document.getElementById("clock").innerHTML = getTime();
}
function add0(i) {
if (i < 10) {
i = "0"+i;
}
return i;
}
function changeInput(newInput) {
input = newInput;
currentSlot = 0;
}
//---------------------------------
</script>
</head>
<body>
<p id="clock" class="textCenter"></p>
<p id="display" class="textCenter"></p>
<p class="textCenter">
<input type="button" onClick="changeInput(P14)" value="1-4 Block">
<input type="button" onClick="changeInput(P17)" value="1-7 Normal">
<input type="button" onClick="changeInput(P57)" value="5-7 Block">
</p>
</body>
</html>
我希望它能够简单地自动检查今天是哪一天,这样您就不必让用户单击按钮。例如 A 天 = 1-7、B 天 = 1-4、C 天 = 1-7、D 天 = 5-7