1

我想从每篇文章(表格)中获取 tr 2, td 4, first a 上的文本,我不能链接到文本,因为当我print_r没有得到任何显示回来时。

// table 1
      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table> 

// table 2
      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table>

// more tables etc.

      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table>  

我的 phpQuery 代码没有错误,但什么也没有显示,我不确定我做错了什么。

<?php
require "phpQuery/phpQuery-onefile.php";


        // Load betting page
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://example.net/');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $html = curl_exec($ch);
        curl_close($ch);

        // Create phpQuery document with returned HTML
        $doc = phpQuery::newDocument($html);

        $articleDate = array();


        $surroundingTheArticles = $doc->find('table.articles');

            foreach( $surroundingTheArticles as $eachArticle)
            { 
             // get table rows    
             $articleDate[]  .= pq($eachArticle)->find('tbody:eq(0) tr:eq(1) td:eq(4)')->text();  // maybe first:a or something - don't know

            }

         print_r($articleDate[1]); 
         // find a way to print all article dates  

?> 
4

1 回答 1

1

此解决方案要求您使用 simple_html_dom。你可以在这里得到它

<?php
require_once 'simple_html_dom.php';

$html = '
<table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table> 

      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table>


      <table class="articles">
         <tbody>
           <tr>some text here maybe tags</tr>
           <tr>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td> some text here maybe tags </td>
              <td><a href="link.html">WANT TO GET THIS TEXT</a></td>
           </tr>
         </tbody>   

      </table>  
';

$html = str_get_html($html);

foreach($html->find('table[class=articles]') as $element){
    $result = $element->find('tr');
    $result = $result[1]->find('td');
    echo($result[4]); echo('<br>');
}


?>
于 2013-08-28T19:35:10.040 回答