1

我拼命尝试访问嵌套 div 中的内容:

<tr>
<th class="monthCellContent" style="vertical-align : top">
    <div class="monthEventWrapper">
        <div class="monthEvent">
            <a class="event"
                href="/event/1"
                title="test title updated - test place - 09:00-10:00">
                    09:00
                    <span class="showForMediumInline">
                        test title updated test place
                    </span>
            </a>
        </div>
    </div>
</th>
</tr>

我正在尝试访问链接中的“09:00”和“测试标题更新测试地点”。

我不知何故被困在

<div class="monthEventWrapper">

我可以访问

$items = $crawler->filter('div[class="monthEventWrapper"]');
print "\n found " . count($items) . " monthEventWrapper divs\n";

found 35 monthEventWrapper divs

但我无法访问

<div class="monthEvent">

$items = $crawler->filter('div[class="monthEvent"]');
print "\n found " . count($items) . " monthEvent divs\n";

found 0 monthEvent divs

我尝试了所有的变化

foreach ($items as $item) {
    foreach ($item->childNodes as $child) {
        $value .= $paragraph->ownerDocument->saveHTML($child);
    }
}

$crawler->filterXPath('//div[@class="monthEvent"]')

没有运气。

html通过验证,没有js。

谢谢 !

4

1 回答 1

0

这是一种解决方法类型的代码:

<?php

use Symfony\Component\DomCrawler\Crawler;
require_once(__DIR__ . '/../vendor/autoload.php');

$html = <<<'HTML'
<!DOCTYPE html>

<html>
    <body>
        <tr>
            <th class="monthCellContent" style="vertical-align : top">
                <div class="monthEventWrapper">
                    <div class="monthEvent">
                        <a class="event"
                            href="/event/1"
                            title="test title updated - test place - 09:00-10:00">
                                09:00
                                <span class="showForMediumInline">
                                    test title updated test place
                                </span>
                        </a>
                    </div>
                </div>
            </th>
        </tr>
    </body>
</html>

HTML;

$crawler = new Crawler($html);
$crawlerFiltered = $crawler->filter('div[class="monthEventWrapper"] a');

$results = [];
$childResults = [];
for ($i=0; $i<count($crawlerFiltered); $i++) {
    $results[] = removeLeadingAndTrailingWhiteCharsAndNewLine($crawlerFiltered->eq($i)->text());

    $children = $crawlerFiltered->eq($i)->children();
    if (count($children)) {
        for ($j=0; $j<count($children); $j++) {
            $childResults[] = removeLeadingAndTrailingWhiteCharsAndNewLine($children->eq($j)->text());
        }
    }
}

$results[0] = substractSpan($results[0], $childResults[0]);

function removeLeadingAndTrailingWhiteCharsAndNewLine(string $text) : string
{
    $pattern = '/(?:\r\n[\s]+|\n[\s]+)/s';
    return preg_replace($pattern, '', $text);
}

function substractSpan($text, $textToSubstract) : string
{
    $length = strlen($text) - strlen($textToSubstract);
    return substr($text, 0, $length);
}

echo 'Parent Nodes:' . PHP_EOL;
var_export($results);
echo PHP_EOL;
echo 'Child Nodes:' . PHP_EOL;
var_export($childResults);

echo PHP_EOL;
echo 'Time: '; 
echo $results[0];

echo PHP_EOL;
echo 'Text: ';
echo $childResults[0];

但给出了这个结果:

Parent Nodes:
array (
  0 => '09:00',
)
Child Nodes:
array (
  0 => 'test title updated test place',
)
Time: 09:00
Text: test title updated test placee

注意我使用for loop->eq(<node-number>)那个给出Crawler的实例而不是DOMNode你通过使用得到的那个foreach

请注意,代码假定所需的文本部分9:00位于开头。

于 2018-11-01T21:47:10.123 回答