我正在尝试使用简单的HTML DOM从Zen-cart商店中按产品部分抓取产品数据。我可以很好地从第一页抓取数据,但是当我尝试加载产品的"下一页"时,该网站会返回索引.php登录页面。
如果我直接将该功能与 *http://URLxxxxxxxxxx.com/index.php?main_page=index&cPath=36&sort=20a&page=2* 一起使用,它会很好地从第 2 页抓取产品信息。
如果我使用 cURL,也会发生同样的事情。
getPrices('http://URLxxxxxxxxxx.com/index.php?main_page=index&cPath=36');
function getPrices($sectionURL) {
$opts = array('http' => array('method' => "GET", 'header' => "Accept-language: enrn" . "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6rn" . "Cookie: zenid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrn"));
$context = stream_context_create($opts);
$html = file_get_contents($sectionURL, false, $context);
$dom = new simple_html_dom();
$dom -> load($html);
//Do cool stuff here with information from page.. product name, image, price and more info URL
if ($nextPage = $dom -> find('a[title= Next Page ]', 0)) {
$nextPageURL = $nextPage -> href;
echo $nextPageURL;
$dom -> clear();
unset($dom);
getPrices($nextPageURL);
} else {
echo "nNo more pages to scrape!!";
$dom -> clear();
unset($dom);
}
}
关于如何解决此问题的任何想法?
我看到很多潜在的罪魁祸首。您没有跟踪cookie或设置引用,simple_html_dom很有可能让您失望。
我的建议是通过小提琴手或查尔斯代理您的请求,并确保它们看起来像来自浏览器的方式。
在循环中传递给函数的下一页URL正在传递&而不是&,file_get_contents不喜欢它。
$sectionURL = str_replace( "&", "&", urldecode(trim($sectionURL)) );