我有这个代码,它将检索$curl_scrapped_page
:中的每个链接
require_once ('simple_html_dom.php');
$des_array = array();
$url = 'http://citeseerx.ist.psu.edu/search?q=mean&t=doc&sort=rlv';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
$html = new simple_html_dom();
$html->load($curl_scraped_page);
然后我想为我废弃的每个链接(在该链接的页面上)获得abstract
。(我也得到了其他东西,比如title
、description
等等,但问题只出在这个abstract
上):
foreach ($html->find('div.result h3 a') as $des) {
$des2 = 'http://citeseerx.ist.psu.edu' . $des->href;
$ch = curl_init($des2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page2 = curl_exec($ch);
libxml_use_internal_errors(true);
$dom = new DomDocument();
$dom->loadHtml($curl_scraped_page2);//line 72
libxml_use_internal_errors(false);
$xpath2 = new DomXPath($dom);
$thing = $xpath2->query('//p[preceding::h3[preceding::div]]')->item(1)->textContent; //line 75
array_push($des_array, $thing);
}
curl_close ($ch);
这是显示代码:
for ($i = 0; $i < 10; $i++) {
echo $des_array[$i];
}
当我在浏览器上查看它时,它三次给了我这个:
Warning: DOMDocument::loadHTML(): Empty string supplied as input in C:'xampp'htdocs'MSP'Citeseerx.php on line 72
Notice: Trying to get property of non-object in C:'xampp'htdocs'MSP'Citeseerx.php on line 75
我意识到我把一个空字符串推到了$des_array
。所以我尝试了这个:
if (empty($thing)){
array_push($des_array,'');
}
else{
array_push($des_array, $thing);
}
这个:if ($thing!=''){..}
。
它仍然给了我那个错误。我该怎么办?谢谢
curl_exec()可能返回false。在这种情况下,请使用curl_error()检查错误是什么。例如,如果href
属性不是以/
开头,您将向curl_init函数传递无效的url。此外,您还可以使用curl_info()来获取有关服务器响应的更多信息
实际上,$curl_scraped_page应该是打开文件的句柄,而不是变量,因为您将传输作为a返回。二进制文件应该读取到文件中,因为它不是字符串