【问题标题】:Scraping class name on a website using php使用 php 在网站上抓取类名
【发布时间】:2020-02-15 05:49:08
【问题描述】:

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

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

我只想scrapecvss6”,尝试了这个:

$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>";
                        
                    }

但它不起作用,它不会在我的网站上显示任何内容。我设法 scrape 它是 html 纯文本。但是,您如何抓取类名中的文本? 谢谢

【问题讨论】:

  • itemscope= 不在您的源字符串中。
  • 对于这些类型的操作,考虑使用像 simplehtmldom.sourceforge.io 这样的 dom 解析器
  • 仅供参考,它是 scrape 不是废品

标签: php web-crawler


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 2013-03-14
相关资源
最近更新 更多