我正在尝试所有的链接,然后转到下一页,直到页面的结尾。我只是不断地得到一个循环。我想我只是有点不知所措,希望今天能再一次得到一些帮助。
getLinks('http://www.homedepot.com/h_d1/N-5yc1vZaqns/h_d2/Navigation?catalogId=10053&langId=-1&storeId=10051&catStyle=ShowProducts#/?c=1&style=List');
function getLinks($URL) {
$html = file_get_contents($URL);
$dom = new simple_html_dom();
$dom -> load($html);
foreach ($dom->find('a[class=item_description]') as $href){
$url = $href->href;
echo $url;
}
if ($nextPage = $dom->find("a[class='paginationNumberStyle page_arrows']", 0)){
$nextPageURL = 'http://www.homedepot.com'.$nextPage->getAttribute('data-url');
$dom -> clear();
unset($dom);
getLinks($nextPageURL);
} else {
echo "\nEND";
$dom -> clear();
unset($dom);
}}
发布于 2013-03-04 10:19:07
在你的代码中,你永远不会跟踪你去过的地方。
假设你从A页开始:
<代码>F29
这个过程将无限重复,因为您最终将一遍又一遍地爬行相同的页面。你需要保留一个你已经抓取过的页面列表,如果你已经抓取过这个页面,就跳过它。
还要注意的是,它可能不是一个像那样的简单循环。
对PHP不是很熟悉,但类似于:
$arr[$url] = true; // Tell it that we know the url
if (array_key_exists($url, $arr)) {
// check if the url exists in the hash
}发布于 2013-03-04 10:35:17
问题是你既要跟随前一个箭头,也要跟随下一个箭头。您的css选择器需要进行调整以解决此问题。
https://stackoverflow.com/questions/15193465
复制相似问题