[英]I have some issues with my crawler in php
my crawler isn't working. 我的搜寻器无法正常工作。 I want it to output
我希望它输出
<div class="content">
But when i run it i get this output: 但是当我运行它时,我得到以下输出:
Array ( )
数组()
This is my code: 这是我的代码:
<?php
$dom = new DOMDocument;
@$dom->loadHTML('https://www.google.com/');
$xp = new DOMXPath($dom);
$links = $xp->query('//div[contains(@class,"content")]');
$result = array();
foreach ($links as $link) {
$result[] = array($link->getAttribute("innerHTML"), $link->nodeValue);
}
print_r($result);
?>
DOMDocument::loadHTML()
parses the HTML from the string that you pass it, see the docs . DOMDocument::loadHTML()
从传递的字符串中解析HTML,请参阅docs 。 In your case the string is https://www.google.com/
which is obviously not valid HTML. 在您的情况下,字符串为
https://www.google.com/
,这显然不是有效的HTML。
You could use cURL to retrieve the actual HTML from the page. 您可以使用cURL从页面检索实际的HTML。 I made a few quick tests and seems that Google's response varies with the user agent, so you could use a real one, eg
我进行了一些快速测试,似乎Google的响应随用户代理的不同而不同,因此您可以使用真实的响应,例如
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36');
$html = trim(curl_exec($ch));
curl_close($ch);
$dom = new DOMDocument;
@$dom->loadHTML($html);
// rest of the code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.