爬取必应首页大图 - PHP
不废话,直接上代码
<?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启个定时器就行了
啰嗦几句
其实也没啥,就是还有python的版本 爬取必应首页大图 - Python