我尝试使用DOMCrawler过滤我的html,但如果找到结果我需要中断。
是否可以从每个中断?
或者,如果我使用foreach,如何获得$node->text();
?或者它不可能?
谢谢
HTML:
<table cellspacing="0" cellpadding="0" align="Center" rules="all" border="1">
<tbody>
<tr>
<td>Entity Name</td>
<td>NV Business ID</td>
<td>Status</td>
<td>Type</td>
</tr>
<tr>
<td><a href="">GOOGLE</a></td>
<td><a href=""></a></td>
<td><a href="">Expired</a></td>
<td><a href="">Reserved Name</a></td>
</tr>
<tr>
<td><a href="">GOOGLE INC.</a></td>
<td><a href="">NV20161275322</a></td>
<td><a href="">Active</a></td>
<td><a href="">Foreign Corporation</a>
</td>
</tr>
</tbody>
</table>
PHP:
$client = new Client();
$client->setHeader('User-Agent', "Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15");
$crawler = $client->request('GET', 'http://nvsos.gov/sosentitysearch/');
$form = $crawler->selectButton('Search')->form();
$crawler = $client->submit($form, array(
'ctl00$MainContent$txtSearchBox' => 'google',
));
if($this->available == false)return;
$crawler->filter('table tr')->each(function (\Symfony\Component\DomCrawler\Crawler $element) {
$status = $element->filter('td')->eq(2)->text();
echo $status;
if ($status == 'Active') {
$this->available = false;
break;
}
});
我想获得身份。然后,如果循环查找状态相等&#34;活动&#34;,我想打破循环。
好的,现在我想出了一些东西。
$crawler = $crawler->filter('#ctl00_MainContent_objSearchGrid_dgCorpSearchResults');
foreach ($crawler as $i => $content) {
$crawler = new Crawler($content);
foreach ($crawler->filter('#ctl00_MainContent_objSearchGrid_dgCorpSearchResults') as $node) {
$longStatus = $node->nodeValue;
echo $longStatus;
}
}
但现在我不知道如何在<td>
中获得第三个<tr>
。
答案 0 :(得分:1)
虽然您无法从$crawler->each()
中断,并且当然不能使用break
退出函数,但是您无法返回任何内容,因此$crawler->each()
将停止迭代,但您仍然有几个选择。
foreach
$crawler->each()
切换到foreach
。
但这比您想象的要简单。只需更换
$crawler->each(function($element) {
your_code();
});
与
foreach($crawler as $content) {
$element = new Crawler($content);
your_code();
}
无需更改代码中的任何内容。您的原始代码变成
...
foreach($crawler->filter('table tr') as $context) {
$element = new Crawler($content);
$status = $element->filter('td')->eq(2)->text();
echo $status;
if ($status == 'Active') {
$this->available = false;
break;
}
}
而且您仍然可以以相同的方式在<td>
中获得第三个<tr>