简体   繁体   English

在PHP中获取HTML元素

[英]Getting HTML element in PHP

I am struck in a problem, i want a small amount of data from a site, but this data is present in different pages. 我遇到了一个问题,我想要一个站点中的少量数据,但是这些数据存在于不同的页面中。

Hence i used curl request to call this pages 因此,我使用curl请求来调用此页面

Problem: 问题:

curl request gives my a html page, and finding that small amount of content is difficult with php as, It is very simple to move through the HTML Dom with JavaScript(jquery). curl请求给了我一个html页面,并且发现使用php很难获得少量内容,因为使用JavaScript(jquery)在HTML Dom中移动非常简单。 Can i move with same ease with php in HTML Dom 我可以用HTML Dom中的php轻松地移动吗

any help would be appreciated , please provide me link of such site also 任何帮助将不胜感激,也请提供我该网站的链接

I am not asking for all the coding , but just a library or link will do 我并不需要所有的编码,但是只需一个库或链接即可

if there is any trick it will also do , a small example like fetching element with ID would be of great help 如果有什么窍门,它也会做的, 一个小例子,比如获取ID为ID的元素将很有帮助。

Thanks 谢谢

Yes you can. 是的你可以。 You can use DomDocument and DomXPath to navigate your HTML tree and search it. 您可以使用DomDocumentDomXPath导航并搜索HTML树。

$doc = new DOMDocument();
$doc->loadHTML($yourHtml); // HTML as string, use loadHTMLFile() to load a file.
$xpath = new DOMXpath($doc);
$elements = $xpath->query("*/div[@id='yourTagIdHere']");
if (empty($elements) || $elements->length == 0)
    echo "Not found";
else
{
    // $elements now contains a DOMNodeList of matching elements
    // which you can foreach() through.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM