php xpath 获取网页a链接 href值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public function handle() { $url = 'http://www.biochemsafebuy.com/m/category_detailed/0' ; $this ->spiderList( $url ); } private function spiderList( $url ){ Log::info( 'list_url' , [ $url ]); $html = file_get_contents ( $url ); $dom = new \DOMDocument(); @ $dom ->loadHTML( $html ); $dom ->normalize(); $xpath = new \DOMXPath( $dom ); //$lists = $xpath->query('.//div[contains(@class, "wrap-img")]/a/text()'); $lists = $xpath ->query( './/div[contains(@class, "wrap-img")]/a/@href' ); foreach ( $lists as $node ) { $detail_url = $node ->nodeValue; Log::info( 'detail_url' , [ $detail_url ]); } } |