Discuz教程网

php下通过curl抓取yahoo boss 搜索结果的实现代码

[复制链接]
authicon 星火燎原 发表于 2011-6-11 08:28:53 | 显示全部楼层 |阅读模式
php下通过curl抓取yahoo boss 搜索结果的实现代码,需要的朋友可以参考下。
1.编写curl类,进行网页内容抓取
代码如下:
  1. class CurlUtil
  2. {
  3. private $curl;
  4. private $timeout = 10;
  5. /**
  6. * 初始化curl对象
  7. */
  8. public function __construct()
  9. {
  10. $this->curl = curl_init();
  11. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
  12. curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  13. curl_setopt($this->curl, CURLOPT_HEADER, false); //设定是否显示头信息
  14. curl_setopt($this->curl, CURLOPT_NOBODY, false); //设定是否输出页面内容
  15. curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
  16. curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
  17. curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
  18. }
  19. /**
  20. * 注销函数 关闭curl对象
  21. */
  22. public function __destruct()
  23. {
  24. curl_close($this->curl);
  25. }
  26. /**
  27. * 获取网页的内容
  28. */
  29. public function getWebPageContent($url)
  30. {
  31. curl_setopt($this->curl, CURLOPT_URL, $url);
  32. return curl_exec($this->curl);
  33. }
  34. }

复制代码


2.创建curl对象
代码如下:
  1. $CurlUtil = new CurlUtil();

  2. 3.抓取yahoo搜索结果
  3. 复制代码 代码如下:
  4. function getYahooSearch(CurlUtil $curl, $key)
  5. {
  6. $key = urlencode($key);
  7. $searchUrl = "http://boss.yahooapis.com/ysearch/web/v1/$key?appid=你的雅虎appid&lang=tzh®ion=hk&abstract=long&count=20&format=json&start=0&count=10";
  8. $josnStr = $curl->getWebPageContent($searchUrl);
  9. $searchDataInfo = json_decode($josnStr, true);
  10. $searchData = $searchDataInfo['ysearchresponse']['resultset_web'];
  11. $returnArray = array();
  12. if (!empty($searchData)) {
  13. foreach ($searchData as $data) {
  14. $returnArray[] = array("url" => $data['url'], "date" => $data['date'], 'title' => strip_tags($data['title']), 'description' => strip_tags($data['abstract']));
  15. }
  16. }
  17. return $returnArray;
  18. }
复制代码
4.测试结果
var_dump(getYahooSearch($CurlUtil, "百度"));




上一篇:php获取后台Job管理的实现代码
下一篇:PHP缩略图等比例无损压缩,可填充空白区域补充色
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 04:59

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表