我想在每篇文章(表格)中获取tr 2,td 4,第一个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
?>
答案 0 :(得分:1)
此解决方案需要您使用simple_html_dom。你可以得到它here
<?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>');
}
?>