如何获得使用“PHP简单的HTML DOM解析器” AJAX产品装载网页上的所有产品细节
问题描述:
所有如何获得使用“PHP简单的HTML DOM解析器” AJAX产品装载网页上的所有产品细节
我使用PHP简单的HTML DOM解析器来获得产品的细节,如标题&价格。 这是代码
<?php
// Include the library
include('simple_html_dom.php');
// Retrieve the DOM from a given URL
$html = file_get_html('http://www.flipkart.com/mobiles/micromax');
// Find all SPAN tags that have a class of "myClass"
foreach($html->find('a.title') as $e){
echo 'Title: '.$e->outertext . '<br>';
//$html = file_get_html('http://www.flipkart.com/mobiles/micromax/'.$e->outertext);
}
foreach($html->find('span.final-price') as $e)
echo 'Price:'.$e->outertext . '<br>';
?>
结果
Title: Micromax X101 (White)
Title: Micromax X291 (White)
Title: Micromax X101 (Yellow)
Title: Micromax X234+ (Wine Red)
Title: Micromax Ninja 3 A57 (Black)
Title: Micromax Ninja 4.0 A87 (Black)
Title: Micromax Bling Q55 (Pearl White)
Title: Micromax X222 (Cocoa Brown)
Title: Micromax X263 (Champagne & Coffee)
Title: Micromax X650 (Silver White)
Title: Micromax A73 (Black)
Title: Micromax X1i XTRA (Black)
Title: Micromax Superfone Lite A75 (Charcoal Black)
Title: Micromax X271 (Black & Blue)
Title: Micromax X50 (Black)
Title: Micromax Q56 (Baby Pink)
Title: Micromax X104 (Black)
Title: Micromax Q22 (Black Green)
Title: Micromax Aisha A52 (Yellow)
Title: Micromax A78 (Coffee)
Price:Rs. 999
Price:Rs. 1910
Price:Rs. 999
Price:Rs. 1190
Price:Rs. 4999
Price:Rs. 6049
Price:Rs. 3130
Price:Rs. 2040
Price:Rs. 1735
Price:Rs. 3350
Price:Rs. 6199
Price:Rs. 1525
Price:Rs. 6299
Price:Rs. 1590
Price:Rs. 4850
Price:Rs. 3999
Price:Rs. 1099
Price:Rs. 1880
Price:Rs. 4699
Price:Rs. 6970
这工作得很好,但在浏览器中http://www.flipkart.com/mobiles/micromax打开此页面。有一个Ajax产品加载功能。
所以我的脚本刚刚获得初始加载产品。我想获得所有的产品。 你可以看到“正在显示1-20 of 78”。如何获得所有78个产品的详细信息?
答
您可以阅读它们的产品数量并除以20,因为它们显示并使用它们的AJAX脚本来获取值。这样可以避免simple_html_dom
和解码json字符串:
http://www.flipkart.com/mobiles/micromax?response-type=json&inf-start=0
http://www.flipkart.com/mobiles/micromax?response-type=json&inf-start=20
等等。
你只需要在页面滚动时检查它被调用的脚本。在Google Chrome中,您可以使用Developer Tools
,以F12打开并观察网络部分。
谢谢,我明白了你的观点。我想从许多电子商务网站获取数据。并且都不使用ajax负载。那么有没有关于使用DOM解析器获取ajax加载数据的建议?原因是我不想编写每个电子商务网站的json解析器。 –
你必须为每个网站做一些不同的事情。 –