gpt4 book ai didi

php - 如何使用php通过id抓取一个div的html内容

转载 作者:可可西里 更新时间:2023-11-01 01:03:04 25 4
gpt4 key购买 nike

我想从我的另一个域中抓取一个 div 的页面包含:

<div id="thisone">
<p>Stuff</p>
</div>

<div id="notthisone">
<p>More stuff</p>
</div>

使用这个 php...

<?php
$page = file_get_contents('http://thisite.org/source.html');
$doc = new DOMDocument();
$doc->loadHTML($page);
foreach ($doc->getElementsByTagName('div') as $node) {
echo $doc->saveHtml($node), PHP_EOL;
}
?>

...给我所有 http://thisite.org/source.html 上的 div , 与 HTML。但是,我只想通过 id 为“thisone”的 div,但使用:

foreach ($doc->getElementById('thisone') as $node) {

不显示任何内容。

最佳答案

$doc->getElementById('thisone');// returns a single element with id this one

尝试 $node=$doc->getElementById('thisone'); 然后打印 $node

附带说明,您可以将 phpQuery 用于类似语法的 jquery:pq("#thisone")

关于php - 如何使用php通过id抓取一个div的html内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18123923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com