【问题标题】:Symfony 2 DOM Crawler. Take text without tagSymfony 2 DOM 爬虫。获取没有标签的文本
【发布时间】:2012-08-07 10:35:23
【问题描述】:

我用这段代码抓取页面:

<br/>

<td class="PropertyBody">
<b>Category:</b>
 Miscellanea: Soft Skill
<br>
<b>Owner:</b>
<a href="mailto:">blabla</a>
<br>
<b>Location:</b>
 bla bla
<br>
<b>Duration:</b>
 6:00
<br>
<b>Max attendees:</b>
 15
<br>
<b>Start at:</b>
 7/19/2012 10:00:00 AM
<br>
<b>Your status:</b>
<br>
</td>

如何使用 Symfony Crawler 从这段代码中提取 '7/19/2012 10:00:00 AM'$crawler-&gt;filter('.PropertyBody &gt; b')-&gt;eq(5)-&gt;text(); 随便'Start at:'

谢谢,我搞定了:

$bigPiece = $crawler->filter('.PropertyBody')->text();
        //getting CATEGORY         
         $pos = strpos($bigPiece, ':')+1;
         $pos2 = strpos($bigPiece, 'Owner:');
         $category = trim(substr($bigPiece, $pos, $pos2-$pos));
         $this->category = $category;
        //getting OWNER
         $pos = strpos($bigPiece, 'Owner:')+6;
         $pos2 = strpos($bigPiece, 'Location:');
         $owner = trim(substr($bigPiece, $pos, $pos2-$pos));
         $training->setOwner($owner);
        //getting LOCATION
         $pos = strpos($bigPiece, 'Location:')+9;
         $pos2 = strpos($bigPiece, 'Duration:');
         $location = trim(substr($bigPiece, $pos, $pos2-$pos));
         $training->setLocation($location);
        //getting DURATION
         $pos = strpos($bigPiece, 'Duration:')+9;
         $pos2 = strpos($bigPiece, 'Max attendees:');
         $duration = trim(substr($bigPiece, $pos, $pos2-$pos));
         $training->setDuration($duration);
        //getting MAXATTENDEES
         $pos = strpos($bigPiece, 'Max attendees:')+14;
         $pos2 = strpos($bigPiece, 'Start at:');
         $maxattendees = trim(substr($bigPiece, $pos, $pos2-$pos));
         $training->setMaxattendies($maxattendees);
        //getting START AT
         $pos = strpos($bigPiece, 'Start at:')+9;
         $pos2 = strpos($bigPiece, 'Your status:');
         $start = trim(substr($bigPiece, $pos, $pos2-$pos));
         $training->setStarts($start);

【问题讨论】:

    标签: php symfony filter web-crawler


    【解决方案1】:

    如果您需要测试这种特殊情况并且您没有能力添加标签,那么您应该考虑使用 PHPUnit 的assertContains()

    $text = $crawler->filter('.PropertyBody > b')->text();
    $this->assertContains('7/19/2012 10:00:00 AM', $text);
    

    【讨论】:

      【解决方案2】:

      添加span 标签。执行以下操作:

      <b>Start at:</b>
      <span class="wantthis">7/19/2012 10:00:00 AM</span>
      

      然后选择它:

      $crawler->filter('.wantthis')->text();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-12
        • 1970-01-01
        相关资源
        最近更新 更多