2012-08-04 28 views
-1

可能重複:
How to parse and process HTML with PHP?

我建設我的網站關閉不同的產品演示,我使用curl 基本上就是我需要做的是讓面臨的幾個問題來自不同網站的一些html部分和我的網站上顯示的例如:標題,模型,描述,用戶評論等...... 我設法完成了一些代碼,但當更改源網址時停止工作......即使源代碼是一樣的 我的代碼:

$url = "http://www.tigerdirect.com/applications/SearchTools/item-details.asp?EdpNo=2819129&CatId=4938"; 

//$url = "http://www.tigerdirect.com/applications/SearchTools/item-details.asp?EdpNo=1808177&csid=_61"; //this one is not working.... 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); 

$source = curl_exec ($ch); 

$start_description1 = "</tr> 
</tbody> 
</table> 




<p>"; 
$end_description1 = "</div> 
</div> 
<div id=\"Videos\" style=\"display:inline;\">"; 
$description1_start_pos = strpos($source, $start_description1) + strlen($start_description1); 
$description1_end_pos = strpos($source, $end_description1) - $description1_start_pos; 
$description1 = substr($source, $description1_start_pos, $description1_end_pos); 
echo $description1; 

它的作品完美,但如果我改變它不會工作,網址... 問題上的HTML代碼不同,其他頁面的START_DESCRIPTION HTML代碼... ...

代替:

</tr> 
</tbody> 
</table> 




<p> 

新的頁面有:

</tr> 
</tbody> 
</table> 


<p> 

或:

</tr> 
</tbody> 
</table> 

<p> 

我該如何避免這個錯誤?或如何避免cUrl錯誤,並檢索我想要的內容?

謝謝!

+0

它不在curl必須解析HTML的功能集內。您需要一個與curl分開的HTML解析器。可能其中一個重複的問題正在幫助你? – hakre 2012-08-04 17:24:07

回答

1

而不是使用strpos,你應該解析html並從html中獲取描述。

對於此應用程序,我建議使用PHP Simple HTML DOM Parser

這裏是它如何工作的例子:

$html = file_get_html('http://www.tigerdirect.com/applications/SearchTools/item-details.asp?EdpNo=1808177&csid=_61'); 
//fetches html content from the url 
$p = $html->find('p', 0); 
//fetches the content of the first <p> element. 

echo $p-> plaintext; 

希望這有助於。