简体   繁体   English

我如何在我的网站之外获取div类的内容

[英]How do I get contents of a div class outside my website

I wanted to get the content of some div classes and echo it back to my website. 我想获取一些div类的内容,并将其回显到我的网站。

This is the example div of a website I'm trying to get 这是我要获取的网站的示例div

<div class="entry-content content">


Hi Goodmorning
<span class="cp-load-after-post"></span>  

</div>

I've already tried some of the codes but non of them worked. 我已经尝试了一些代码,但是没有一个起作用。

$url = 'https://thewebsiteimtryingtogetcontent.com';
$content = file_get_contents($url);
$first_step = explode( '<div id="iwanttogetthisid">' , $content );
$second_step = explode("</div>" , $first_step[1] );

But then when I try this nothing echoes back, please help I'm new to php. 但是,当我尝试此操作时,没有任何回音,请帮助我是php的新手。

The DomCrawler component eases DOM navigation for HTML and XML documents. DomCrawler组件简化了HTML和XML文档的DOM导航。

https://symfony.com/doc/current/components/dom_crawler.html https://symfony.com/doc/current/components/dom_crawler.html

composer require symfony/dom-crawler
require __DIR__.'/vendor/autoload.php';

use Symfony\Component\DomCrawler\Crawler;

$html = <<<'HTML'
<div class="content">


Hi Goodmorning
<span class="cp-load-after-post"></span>  

</div>
HTML;

$crawler = new Crawler($html);

echo $message = $crawler->filterXPath('//div[@class="content"]')->text();

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

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