获取具有cUrl-cUrl问题的html源的特定部分检索正确的内容cUrl_setopt($ch,CURLOPT_URL


get specific portion of html source with cUrl - cUrl problems retrieving right content, curl_setopt($ch, CURLOPT_URL,$url);

可能重复:
如何使用PHP解析和处理HTML?

我正在用不同产品的演示来构建我的网站,我在使用curl时遇到了一些问题基本上,我需要做的是从不同的网站获取html的一些部分,并在我的网站上显示,例如:标题、模型、描述、用户评论等。。。。我设法完成了一些代码,但当更改源url时,就停止了工作。。。即使来源也是一样的我的代码:

$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;

它工作得很好,但如果我更改网址,它就不起作用。。。问题是startdescription html代码。。。在其他页面上,html代码有所不同。。。

而不是:

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


<p>

新页面有:

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

<p>

或:

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

我怎样才能避免这个错误?或者如何避免cUrl错误,并检索我想要的内容?

谢谢!

不应该使用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;

希望这能有所帮助。