爬取必应首页大图 - PHP
侧边栏壁纸
  • 累计撰写 114 篇文章
  • 累计收到 21 条评论

爬取必应首页大图 - PHP

SanLiLin
2019-02-27 / 0 评论 / 523 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2019年03月05日,已超过1476天没有更新,若内容或图片失效,请留言反馈。

不废话,直接上代码

<?php
 /**
 * 获取bing首页大图保存在本地
 * @Author: Wang Hongbin
 * @Email: wanghongbin@ngoos.org
 * @Date:   2018-02-24 15:21:40
 * @Last Modified by:   Wang Hongbin
 * @Last Modified time: 2018-02-24 15:36:52
 */
class GetBingPic {
    
    private $bingApiXml;
      private $bingApiJson;

    function __construct()
    {
        // 返回xml格式
        $this->bingApiXml = "http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1";

        // 返回json格式
        $this->bingApiJson = "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";
    }

    /**
     * xml格式数据
     * @return [type] [description]
     */
    public function getDataXml()
    {
        $url = $this->bingApiXml;
        $data = $this->httpGet($url);
        return $data;
    }

    /**
     * json格式数据
     * @return [type] [description]
     */
    public function getDataXml()
    {
        $url = $this->bingApiJson;
        $data = $this->httpGet($url);
        return $data;
    }

    private function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);

        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }
}

$BingPic = new GetBingPic();
$xmldata = $BingPic->getDataXml();
var_dump($xmldata);

爬取结果

下图是七月份至今的爬取图片,因为是在window上执行的,电脑不开机的时候不会执行,代码放在Linux上执行也没问题,使用crontab启个定时器就行了

https://wx4.sinaimg.cn/large/0064eL5bly1g0krbnliu4j30wu0gkwxp.jpg

啰嗦几句

其实也没啥,就是还有python的版本 爬取必应首页大图 - Python
0

评论 (0)

取消