zoukankan      html  css  js  c++  java
  • 爬取必应首页大图

    不废话,直接上代码

    <?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://cdn.jsdelivr.net/gh/WHBLeer/Gallery/img/20201124112720.png

    啰嗦几句

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

  • 相关阅读:
    收藏夹
    获取某个元素在页面上的偏移量
    React多行文本溢出处理(仅针对纯文本)
    react
    CDN初学搭建(ats)
    linux查看cpu、内存、版本信息
    MySQL5.6版本性能调优my.cnf详解
    How to install cacti on centos 6
    win10安装.net3.5 报错解决
    CentOS6.5安装Cacti统计图乱码解决
  • 原文地址:https://www.cnblogs.com/sanlilin/p/14145169.html
Copyright © 2011-2022 走看看