gpt4 book ai didi

javascript - 从 JavaScript 更新的 html 中读取数据

转载 作者:行者123 更新时间:2023-11-29 20:25:18 25 4
gpt4 key购买 nike

是否可以在this website中阅读实时更新的报价?通过 actionscript3,而不必一直重新加载页面,这会占用大量网络资源。

换句话说,是否可以将报价数据直接流式传输到 actionscript3 或最终从 javascript 流式传输到 PHP?

如果没有,有没有人对如何将这些引语放入 actionscript 有任何建议?

最佳答案

扩展 Theo 所说的...(我敢肯定有一种更巧妙的方法可以做到这一点,我会被谴责为黑客,但它确实有效)

我基本上只是提取 EUR/USD 下红色的前两个数字。

这是要放在您的服务器上的名为 getContent.php 的 php 脚本

<?php

$handle = fopen("getVars.php", "r");

$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}

$vars = explode("&", $contents);
$time = substr($vars[2], 5);
$difference = abs(date(s)-$time);

if($difference>5)
{

$handle = fopen("http://www.fxstreet.com/technical/currencies-glance/pair.aspx?id=EUR/USD", "r");

$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}

$contents=trim($contents);
$pos1 = strpos($contents, 'lhtml_0" innerOnUpdate="att=BID" innerfilter="format_number">');
$str1 = substr($contents, $pos1, 100);
$cur1 = substr($str1, 61, 6);
$pos2 = strpos($contents, 'lhtml_1" innerOnUpdate="att=ASK" innerfilter="format_number">');
$str2 = substr($contents, $pos2, 100);
$cur2 = substr($str2, 61, 6);

$cachedPage = fopen("getVars.php", "w");
$varString = "cur1=$cur1&cur2=$cur2&time=".date(s);
fwrite($cachedPage,$varString);
fclose($cachedPage);
echo "cur1=$cur1&cur2=$cur2&cached=false";
}
else
{
$handle = fopen("getVars.php", "r");

$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}

echo $contents."&cached=true";
}

fclose($handle);
?>

然后是 ActionScript

var updateTimer:Timer = new Timer(5000);
updateTimer.addEventListener(TimerEvent.TIMER, getQuotes);
updateTimer.start();
function getQuotes(e:Event):void
{
var request:URLRequest = new URLRequest ("getContent.php");
var loader:URLLoader = new URLLoader (request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);
}

function onComplete (event:Event):void
{
var variables:URLVariables = new URLVariables( event.target.data );
currency1.text = variables.cur1;
currency2.text = variables.cur2;
}
var e:Event;
getQuotes(e);

您可以在这里看到它的实际效果... http://www.hupcapstudios.com/getCurrency.swf

破解部分是我用 php 解析页面。我不得不做一些严肃的子字符串操作。我敢肯定,任何具有一定解析能力的人都可以编写更简洁的代码来提取您需要的所有数据。

我只是想尝试一下。祝你好运 :)

关于javascript - 从 JavaScript 更新的 html 中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1521088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com