php抓取阿里巴巴商品详情,php+phpquery简易爬虫抓取京东商品分类

这是一个简单的php加phpquery实现抓取京东商品分类页内容的简易爬虫。phpquery可以非常简单地帮助你抽取想要的html内容,phpquery和jquery非常类似,可以说是几乎一样;如果你有jquery的基础的话你可以迅速地上手。

1、下载phpquery并置于web根目录下的phpQuery文件夹

phpquery下载:https://code.google.com/p/phpquery/downloads/list

phpquery教程可在这里查看:https://code.google.com/p/phpquery/

2、抓取程序

69c5a8ac3fa60e0848d784a6dd461da6.png

*

* To change the template for this generated file go to

* Window - Preferences - PHPeclipse - PHP - Code Templates*/

header("Content-type:text/html; charset=utf-8");function getPage( $url)

{$cnt = file_get_contents($url);return mb_convert_encoding($cnt ,"UTF-8","GBK");

}include 'phpQuery/phpQuery.php';

$url = 'http://www.jd.com/allSort.aspx';

$page = getPage($url);//phpQuery::newDocumentHTML($page);

phpQuery::newDocumentFile($url);$firstCate = pq('#allsort .m');$id = 0;foreach($firstCate as $first){$id ++;$topcate = pq($first)->find(".mt a");//echo "**************************" . $topcate->text() . "**************************************";

echo $id . "#";foreach($topcate as $top){echo pq($top)->text() . "#" . "< a href='" .pq($top)->attr("href") . "' target='_blank'>" . pq($top)->text() ."< /a>、";

}echo "#0#1";

$companies = pq($first)->find(".mc dl");

$parent_id = $id;foreach($companies as $company)

{$id++;$sparent_id = $id;echo "  " . $id . "#" .pq($company)->find('dt')->text() . "#" . "< a href='" . pq($company)->find('dt a')->attr("href") . "' target='_blank'>" . pq($company)->find('dt')->text() ."< /a>#" . $parent_id ."#2
";$cate = pq($company)->find('dd em a');foreach($cate as $detail) {$id++;echo "    " . $id . "#" .pq($detail)->text() . "#" . "< a href='". pq($detail)->attr("href") . "' target='_blank'>" . pq($detail)->text() ."< /a>#" . $sparent_id . "#3
";

}

}

}?>

69c5a8ac3fa60e0848d784a6dd461da6.png

3、运行效果

45becd1ff0b6cb1f0e25002726e19e61.png

这样可以抓取京东商品分类的信息了。可以加上数据库,将数据保存在数据库中,这样可以更利于数据的保存和操作。虽然这里只是抓取京东商品的分类,如果延伸一下的话还可以抓取商品价格,好评差评等信息。这里就不一一细说了,具体问题具体解决,完全看需求。如果有需要的话还可以做成万能的,输入标签的xpath,然后得到具体的值;这纯属YY,有兴趣的可以网上找找资料,实现的方式应该也不少。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值