bilibili用户头像下载

2018年1月31日 5151点热度 3人点赞 0条评论

[title]API[/title]

url http://space.bilibili.com/ajax/member/GetInfo

method POST

value mid=用户uid

Referer http://space.bilibili.com/用户uid/

Content-Type application/x-www-form-urlencoded; charset=UTF-8

Response
{
    "status": true,
    "data": {
        "mid": "2",
        "name": "碧诗",
        "approve": true,
        "sex": "男",
        "rank": "10000",
        "face": "http://i2.hdslb.com/bfs/face/6f3e8f3f7abd1ed4ff96508e79a583d0519f1b3c.jpg",//这就是地址
        "coins": 0,
        "DisplayRank": "32000",
        "regtime": 1245824205,
        "spacesta": 0,
        "place": "上海市 浦东新区",
        "birthday": "0000-09-19",
        "sign": "",
        "description": "",
        "article": 0,
        "level_info": {
            "next_exp": "-",
            "current_level": 6,
            "current_min": 28800,
            "current_exp": 48149
        },
        "pendant": {
            "pid": 32,
            "name": "影帝",
            "image": "http://i2.hdslb.com/bfs/face/b4f8507dad535a8e66adced28202615d2def0839.png",
            "expire": 1566497861
        },
        "nameplate": {
            "nid": 10,
            "name": "见习偶像",
            "image": "http://i1.hdslb.com/bfs/face/e93dd9edfa7b9e18bf46fd8d71862327a2350923.png",
            "image_small": "http://i2.hdslb.com/bfs/face/275b468b043ec246737ab8580a2075bee0b1263b.png",
            "level": "普通勋章",
            "condition": "所有自制视频总播放数>=10万"
        },
        "official_verify": {
            "type": 0,
            "desc": "bilibili创始人(站长)"
        },
        "toutu": "bfs/space/768cc4fd97618cf589d23c2711a1d1a729f42235.png",
        "toutuId": 1,
        "theme": "default",
        "theme_preview": "",
        "im9_sign": "82d2a1719bf9e3919f910c7d8100a2bb",
        "playNum": 9174901,
        "fans_badge": true
    }
}

[title]PHP获取用户头像[/title]

curl模拟POST
function curlPost( $mid ) {
	$url     = "http://space.bilibili.com/ajax/member/GetInfo";
	$referer = "http://space.bilibili.com/$mid";
	$value   = "mid=$mid";

	$header    = array();
	$header[0] = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8";

	$ch = curl_init();
	curl_setopt( $ch, CURLOPT_URL, $url );
	curl_setopt( $ch, CURLOPT_HEADER, 0 );
	curl_setopt( $ch, CURLOPT_REFERER, $referer );
	curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt( $ch, CURLOPT_POST, 1 );
	curl_setopt( $ch, CURLOPT_POSTFIELDS, $value );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
	$data = curl_exec( $ch );
	curl_close( $ch );

	return $data;
}
解析Json
$obj = json_decode( $json );
$url = $obj->data->face;
下载用户头像
function faceDown( $url, $mid ) {
	$cdn = array(
		"121.14.89.216",//国内CDN节点,由站长工具ping获得
		"121.14.89.215",
		"101.227.209.214",
		"121.14.89.212",
		"36.102.211.230",
		"183.57.85.133",
		"121.14.89.212",
		"180.163.159.14",
		"157.255.40.101",
		"139.227.230.31",
		"122.225.67.224",
		"124.193.226.101",
		"180.163.159.14",
		"113.107.239.104",
		"183.66.67.158",
		"61.185.47.227",
		"42.120.107.18",
		"61.155.221.135",
		"119.97.134.252",
		"121.205.6.234",
		"61.158.144.101"
	);
	/*$cdn = array(
		"47.89.67.201",//国外CDN节点
		"66.102.255.35",
		"66.102.255.36",
		"66.102.255.31",
		"47.89.67.201",
		"47.89.67.205",
		"66.102.255.35",
		"66.102.255.60",
		"66.102.255.31"
	);*/
	$src = "http://" . $cdn[ array_rand( $cdn ) ] . "/";
	$src .= str_replace( "http://", "", $url );

	$pic = file_get_contents( $src );
	if ( file_put_contents( "headicon/face_$mid.jpg", $pic ) ) {
		echo "face picture saved\r\n";
	}
}
防止封IP
  • 代理
  • 延迟
function aSleep() {
	$s = rand( 1, 5 );
	sleep( $s );
}

[title]缺点[/title]

  • 单线程(POST过快会封IP)
  • 缺少经费

[title]成果[/title]

下载了uid1-uid1000000的头像。

使用了4台服务器同时下载,花费约45w秒。

因一台服务器磁盘下满了,有不明数量的图片下载失败。

4个压缩包共34.5GB。

数据表在这里:

立即下载

KAMINO

这个人很懒,什么都没留下

文章评论