我正在尝试从网站抓取数据,但我也这样做了,但问题是加载更多按钮,我只能爬网可见数据,在单击load-ofor of-load-我无法爬网的更多按钮。
使用preg_match_all:
$page = file_get_contents('https://www.healthfrog.in/chemists/medical-store/gujarat/surat');
preg_match_all(
'/<h3><a href="(.*?)">(.*?)</a></h3><p><i class="fa fa-map-marker"></i>(.*?)</p>/s',
$page,
$retailers, // will contain the article data
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($retailers as $post) {
$retailer['name'] = $post[2];
$retailer['address'] = $post[3];
echo "<b>".$retailer['name']."</b><br/>".$retailer['address']."<br/><br/>";
}
使用domdocument:
$html = new DOMDocument();
@$html->loadHtmlFile('https://www.healthfrog.in/chemists/medical-store/gujarat/surat');
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query('//*[@id="setrecord"]/div[@class="listing "]');
foreach ($nodelist as $n){
$retailer = $xpath->query('h3/a', $n)->item(0)->nodeValue."<br>";
$address = $xpath->query('p', $n)->item(0)->nodeValue;
echo "<b>".$retailer."</b><br/>".$address."<br/><br/>";
}
任何想法如何一次获取全数据?
我认为您需要尝试以更有效的方式爬行网页。
我对您的第一个建议是将phantomjs用作命令行中的复杂网络引擎。这意味着您可以执行Phantom JS操作(在JavaScript中)获取网页,触发某些DOM事件并使用PHP Exec命令获得所需的数据。
phantomjs是具有JavaScript API的无头Webkit。它对各种网络标准都有快速和本地的支持:DOM处理,CSS选择器,JSON,CANVA和SVG。
// Simple Javascript example
console.log('Loading a web page');
var page = require('webpage').create();
var url = 'http://phantomjs.org/';
page.open(url, function (status) {
//Do your dom operations( click read more button or anything else) and just console.log(yourDataThatYouNeed)
phantom.exit();
});
要获取数据,您需要为phantomjs的php驱动程序。
在这里,phantomjs => https://github.com/jonnnnyw/php-phantomjs
实际上,我有一个用于phantomjs的php驱动程序,我开发了作为一个附带项目,我计划在接下来的几天内在我的github帐户上发布。
我建议您的第二种方法(坦率地说,对于复杂项目的意见正确)是使用刮擦框架之类的刮擦框架。您可以查看文档,了解如何用砂纸从网页上刮擦数据。
scrapy是基于Python的网站提取所需数据的有力框架。
您可以查看本教程的使用scrapy https://docs.scrapy.org/en/latest/introro/tutorial.html