有没有办法获得脚本和样式表的绝对URL? 对于链接,可以通过简单的方式完成:
$crawler = new Crawler($html, $url);
$crawler->filter('a')->each(function (Crawler $node, $i) {
$href = $node->link()->getUri();
});
但它对js和css不起作用。
答案 0 :(得分:1)
你试过这个吗?
$crawler = new Crawler($html, $url);
$crawler->filter('link')->each(function (Crawler $node, $i) {
$href = $node->attr('href');
});
<强> UPDATE2 强>
$html = <<<'HTML'
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="apple-touch-icon" type="image/png" href="/apple-touch-icon.png">
<script type="text/javascript" src="/js/v5/all.js?v=4"></script>
</head>
<body>
<img src="/images/v5/logos/header-logo.svg" alt="Symfony" class="img-responsive" />
<p class="message">Hello World!</p>
<p>Hello Crawler!</p>
</body>
</html>
HTML;
$crawler = new Crawler($html);
$websiteName = 'http://myCoolWebsite.com';
var_dump($websiteName . $crawler->filter('link')->first()->attr('href'));
var_dump($websiteName . $crawler->filter('img')->first()->attr('src'));
var_dump($websiteName . $crawler->filter('script')->first()->attr('src'));
输出:
string(45) "http://myCoolWebsite.com/apple-touch-icon.png"
string(56) "http://myCoolWebsite.com//images/v5/logos/header-logo.svg"
string(41) "http://myCoolWebsite.com/js/v5/all.js?v=4"