使用 php 抓取网站上的类名



所以,我想从网站上抓取一个类名。以下是 html 代码的来源:

<td title="Complexity" class="cvss6" itemscope itemtype="http://schema.org/Rating">

我只想刮">cvss6",尝试过这个:

$nilai1 = explode('<td title="Complexity" class="', $kodeHTML);
$nilai_show2 = explode('" itemscope="', $nilai1[1]);echo "

<tr><td width='85%' align='left' bgcolor='#F5F5F5'>".$judul_show[0]."</td>";

if($nilai_show2[0] == 'cvss6') {
echo "<td width='15%' align='center' bgcolor='#FF0000'>High</td></tr>";

}

但它没有用,它只是不会在我的网站上显示任何内容。我设法抓取了它的 html 纯文本。但是如何抓取类名中的文本呢? 谢谢

要回答您的问题,您可以使用正则表达式来查找所需的内容, 使用下面的代码,我们尝试找到期望 class="something" 的术语,并带有多行标志 (https://www.php.net/manual/fr/function.preg-match-all.php(:

preg_match_all(
'/class="(.+?)"/m',
'<b>exemple : </b><div class="test test1 test2 test3" align=left>This is a test</div class="t1 t2">',
$out
);
var_dump($out[1]);
/* output
array(2) {
[0]=>
string(22) "test test1 test2 test3"
[1]=>
string(5) "t1 t2"
}
*/

另外,我建议您使用库来抓取带有php的网页。

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

最新更新