我有这段代码将检索以下中的每个链接$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!=''){..}
。
它仍然给了我这个错误。我应该怎么办?谢谢..