2011-01-06 65 views
0

是否有一種方法可以從頁面中的某個<tr>標記以動態方式獲取文本?從某個<tr>標記中獲取文本

例如我有一個頁面,其中<tr>的值爲「a1」。我只想從此<tr>標籤中獲取文本,並將其回顯到頁面中。這可能嗎?

這裏是HTML:

<html><tr id='ieconn2' > 
    <td><table width='100%'><tr><td valign='top'><table width='100%'><tr><td><script type="text/javascript"><!-- 
google_ad_client = "pub-4503439170693445"; 
/* 300x250, created 7/21/10 */ 
google_ad_slot = "7608120147"; 
google_ad_width = 300; 
google_ad_height = 250; 
//--> 
</script> 
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script><br>When Marshall and Lily fear they will never get pregnant, they see a specialist who can hopefully help move the process along. Meanwhile, Robin starts her new job.<br><br><b>Source: </b>CBS 

<br>&nbsp;</td></tr><tr><td><b>There are no foreign summaries for this episode:</b> <a href='/edit/shows/3918/episode_foreign_summary/?eid=1065002553&season=6'>Contribute</a></td></tr><tr><td><b>English Recap Available: </b> <a href='/How_I_Met_Your_Mother/episodes/1065002553?show_recap=1'>View Here</a></td></tr></table></td><td valign='top' width='250'><div align='left'> 
<img alt='How I Met Your Mother season 6 episode 13' src="http://images.tvrage.com/screencaps/20/3918/1065002553.jpg" width="248" border='0' > 
</div><div align='center'><a href='/How_I_Met_Your_Mother/episodes/1065002553?gallery=1'>6 gallery images</a></div></td></tr></table></td></tr><tr> 
    <td background='/_layout_v3/buttons/title.jpg' height='39' width='631' align='center'> 
<table width='100%' cellpadding='0' cellspacing='0' style='margin: 1px 1px 1px 1px;'> 
<tr> 
<td align='left' style='cursor: pointer;' onclick="SwitchHeader('ieconn3','iehide3','26')" width='90'>&nbsp;<span style='font-size: 15px; font-weight: bold; color: black; padding-left: 8px;' id='iehide3'><img src='/_layout_v3/misc/minus.gif' width='26'></span></td> 
<td align='center' style='cursor: pointer;' onclick="SwitchHeader('ieconn3','iehide3','26')" ><h5 class='nospace'>Sponsored Links</h5><a name=''></a></td> 

<td align='left' width='90' >&nbsp;</td></tr></table></td> 
</tr></html> 

所有我想要得到的是這樣的文字:「當馬歇爾和Lily擔心他們將永遠不會懷孕,他們看到一個專家誰可以希望幫助一起移動的過程。同時,羅賓開始她的新工作「

+0

你能舉一個你想要的例子嗎? – lonesomeday 2011-01-06 23:44:55

+0

將只有​​。你想得到​​的價值嗎?想要使用jQuery – Praneeth 2011-01-06 23:46:48

+0

@Praneeth在PHP中使用jQuery非常困難。 – lonesomeday 2011-01-06 23:49:46

回答

3

這個怎麼樣?

$dom = new DomDocument; 
libxml_use_internal_errors(true); 
$dom->loadHTMLFile(...); 
libxml_clear_errors(); 

$xpath = new DomXpath($dom); 
$nodes = $xpath->query('/html/body/tr/td/table/tr/td/table/tr/td'); 
foreach ($nodes as $node) 
{ 
    echo $node->nodeValue, "\n"; 
} 
2

如果我假設你想要做的權利你可以到以下內容:。

$url = 「http://url.tld」; 
$str = file_get_contents($url); 

,並從那裏只是使用PHP的字符串functi可以刪除你不喜歡的部分(可能會生成一個正則表達式來加速進程)。

如果上述方法不工作,你可以嘗試這樣的更復雜的功能:

function get_url_contents($url){ 
    $crl = curl_init(); 
    $timeout = 5; 
    curl_setopt ($crl, CURLOPT_URL,$url); 
    curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 
    $ret = curl_exec($crl); 
    curl_close($crl); 
    return $ret; 
}