gpt4 book ai didi

PHP 从 HTML 类中获取文本

转载 作者:行者123 更新时间:2023-11-28 03:28:54 25 4
gpt4 key购买 nike

我正在做一个项目,我需要从 HTML 类中获取所有数据。

<div class="sprofile">
<a href="http://www.cibap.nl/profile/2127/"><b>Jeroen Rinzema</b></a>
<br />
<i>Reclame en Media</i>
<br />
Klas RMM21C
<div class="istatus offline">
Offline
</div>
</div>

这是我需要从 HTML 页面获取的 HTML。但我不知道如何在 PHP 中选择 HTML 文档的这一部分。我需要使用的部分是:Jeroen RinzemaKlas RMM21C

如果您想查看完整的 HTML 文档,可以这样做吗 here .

到目前为止我尝试过的是:

$url = "http://www.cibap.nl/profile/$username";
$html = file_get_html($url);
foreach($html->find('div.sprofile') as $article){
$item['name'] = $article->find('div.sprofile a b', 0)->plaintext;
$item['klas'] = $article->find('div.sprofile', 0)->plaintext;
}
echo'done';

最佳答案

使用 Symfony DomCrawler ComponentCssSelector Component .它提供了一种非常自然的语法,并允许您根据元素的类来选择元素。

如果不需要 PHP,我可以建议使用 nodejs (有或没有 jQuery)?

关于PHP 从 HTML 类中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19279606/

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