2013-04-02 62 views
0

此代碼位於外部網址:www.example.com。從外部頁面獲取值?

</head><body><div id="cotizaciones"><h1>Cotizaciones</h1><table cellpadding="3" cellspacing="0" class="tablamonedas"> 
    <tr style="height:19px"><td class="1"><img src="../mvd/usa.png" width="24" height="24" /></td> 
    <td class="2">19.50</td> 
    <td class="3">20.20</td> 
    <td class="4"><img src="../mvd/Bra.png" width="24" height="24" /></td> 
<td class="5">9.00</td> 
<td class="6">10.50</td> 
    </tr><tr style="height:16px" valign="bottom"><td class="15"><img src="../mvd/Arg.png" width="24" height="24" /></td> 
<td class="2">2.70</td> 
<td class="3">3.70</td> 
<td class="4"><img src="../mvd/Eur.png" width="24" height="24" /></td> 
<td class="5">24.40</td> 
<td class="6">26.10</td> 
</tr></table> 

我想要得到td的值,有什麼建議嗎? php,jquery等

+0

你是如何得到這些數據的?使用PHP或使用JavaScript? –

+0

由於[相同來源策略](http://en.wikipedia.org/wiki/Same_origin_policy),瀏覽器將不允許使用JavaScript。 –

+0

我不介意如果它的PHP或JavaScript,我想獲得該值 – jandresrodriguez

回答

4

由於安全限制只允許您從您自己的網站加載數據,您將無法使用javascript來做到這一點。

你將不得不用php(使用像file_get_contents這樣簡單的東西)來拉內容,然後解析它。

對於分析,採取讀通過這個綜合後:

How do you parse and process HTML/XML in PHP?

DOM很可能將是你最好的選擇。

嘗試玩這個周圍:

$html = file_get_contents('/path/to/remote/page/'); 
$dom = new DOMDocument; 
$dom->loadHTML($html); 
foreach ($dom->getElementsByTagName('td') as $node) { 
    echo "Full TD html: " . $dom->saveHtml($node) . "\n"; 
    echo "TD contents: " . $node->nodeValue . "\n\n"; 
} 
0

它不可能做的jQuery,但是你可以很容易地用PHP做。

使用file_get_contents將頁面的整個源代碼讀取到字符串中。

解析標記包含整個頁面源的字符串以獲取所有td值。

<?php 
$srccode = file_get_contents('http://www.example.com/'); 
/*$src is a string that contains source code of web-page http://www.example.com/ 

/*Now only thing you have to do is write a function say "parser" that tokenise or parse the string in order to grab all the td value*/ 

$output=parser($srccode); 
echo $output; 

?> 

你必須非常小心,在分析,從而獲得所需輸出時解析您可以使用正則表達式或創建的字符串自己查找table.You可以用寫在PHP5一個HTML DOM解析器,讓你以一種非常簡單的方式操作HTML。許多這樣的免費解析器都可用。