doufei8250 2014-07-15 10:19
浏览 26
已采纳

简单-html-dom的后备规则

I'm trying to write a small scraper to do things more automatically,
so far it's been doing the job nearly perfect - i'm missing fallback rules for older pages)

i'll try to explain myself,
currently i'm picking up title with $html->find('h1[class=TitWhite22]')
but some of the pages contain older format where instead of being on class=Titwhite22 it might appear with class=textwhite16 (or any other class for that matter).

my basic function is:

foreach($html->find('h1[class=TitWhite22]') as $element){
    $titleName = $element->innertext;
}

how can i set a rule that if the specific class isn't there, it should attempt a different class ?

  • 写回答

1条回答 默认 最新

  • donglin7979 2014-07-15 13:16
    关注

    The below code may resolve your problem.

    $count22 = 0;
    
    foreach($html->find('h1[class=TitWhite22]') as $element)
    { 
     $count22 = $count22+1;
    }
    
    if($count22==0)
    {
      $count16=0;
      foreach($html->find('h1[class=TexttWhite16]') as $element1)
      {
        $count16 = $count16+1;
      }
      if($count16==0)
      {
        $count12=0;  
        foreach($html->find('h1[class=TitWhite12]') as $element2)
        {
          $count12=$count12+1;
        }
        if($count12==0)
          echo "All Fail";
        else
        {
          foreach($html->find('h1[class=TitWhite12]') as $element2)
          {
            $titleName12= $element2->innertext;
          }
        }
      }
      else
      {
        foreach($html->find('h1[class=TexttWhite16]') as $element1)
        {
            $titleName16= $element2->innertext;
        }
      }  
    }
    else
    {
       foreach($html->find('h1[class=TitWhite22]') as $element)
       { 
         $titleName22= $element->innertext;
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog