我有这项方案,有了一个开关:
$htmlContent = file_get_contents( http://somesite.com );
$htmlDOM->loadHTML( $htmlContent );
$htmlXPath = new DOMXPath( $htmlDOM );
for($i = 0; $i <= 16; $i++ ) {
switch($i) {
case 0:
$link = utf8_decode($htmlXPath->query( /html/body/div[2]/div[2]/div/div/div/div/div[2]/div/a )->item(0)->getAttribute( href ));
break;
case 1:
$link = utf8_decode($htmlXPath->query( /html/body/div[2]/div[2]/div/div/div/div/ul/li/div/a )->item(0)->getAttribute( href ));
break;
default:
$link = utf8_decode($htmlXPath->query( /html/body/div[2]/div[2]/div/div/div/div/ul/li[ . $i . ]/div/a )->item(0)->getAttribute( href ));
break;
}
}
对于案件0和案件1,如预期,但因违约,将出现这一错误:
PHP Fatal error: Call to a member function getAttribute() on a non-object
我可以想象,这是因为一美元而发生的,但我如何能够解决这一问题?
帮助!