loading...

精華 (PHP)小紅書無水印視頻/圖集解析實現(xiàn)思路

本帖最后由 編輯 于4 個月前編輯 只看Ta 樓主

直接貼代碼:

class Video {
    public function redbook($url) {
        $ip= $this->get_randip();
        $array = parse_url($url);
        switch ($array['host']) {
            case 'www.xiaohongshu.com':
                $id = str_replace('/explore/' , "", $array['path']);
                $loc = 'https://www.xiaohongshu.com/discovery/item/'."$id";
                break;
            case 'xhslink.com':
                $loc=get_headers($url,true)['Location'][0];
                //print_r($loc);
                $array = parse_url($loc);
                $id = str_replace('/discovery/item/' , "", $array['path']);
                break;
            default:
                exit(json_encode(['code'=>-1, 'msg'=>"視頻鏈接不正確"], 480));
                break;
        }
        $header =array(
              'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0',
              'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
              'Referer: https://www.xiaohongshu.com/',
              'Cookie: abRequestId=9b9a13ea-1720-51e9-a6de-331abacaa69e; webBuild=4.9.0; a1=18eb6b4d5afmq46r71nf5oo7195et621myj77skjs50000382501; webId=3c8dab9561d74f401b898f922ccbc720; gid=yYdDKD4fSy6iyYdDKD4f2vMT0i6A4KFWyIi2VVMllWyj2W28d9KJyW888qYJ28y8DJ00dYYj; unread={%22ub%22:%2266037cba000000000d00f96c%22%2C%22ue%22:%2265f38c00000000000d00cbc9%22%2C%22uc%22:29}; xsecappid=xhs-pc-web; websectiga=7750c37de43b7be9de8ed9ff8ea0e576519e8cd2157322eb972ecb429a7735d4; sec_poison_id=abc0abe9-c5e3-4933-a48b-33aeacb1b480; web_session=0400698d0903920b8e59620d27344be55719de',
              'Connection: keep-alive',
              'CLIENT-IP:'.$ip,
              'X-FORWARDED-FOR:'.$ip
           );
        $html = $this->curl_general($loc,$header);
        //正則 window.__INITIAL_STATE__=
        preg_match('/window\.__INITIAL_STATE__.+redmojiMap\"\:\{\}\}\}\}/',$html,$arr);
        $arr = str_replace('window.__INITIAL_STATE__=' , "", $arr[0]);
        $json = json_decode(str_replace('undefined' ,'null', $arr),true);
        //print_r($json);
        $images_num=count($json['note']['noteDetailMap'][$id]['note']['imageList']);
        for($i = 0; $i < $images_num; $i++){
            $ch[$i] = $json['note']['noteDetailMap'][$id]['note']['imageList'][$i]['infoList'][1]['url'];
        }
        if(empty($json['note']['noteDetailMap'][$id]['note']['video'])){
            $arr = array(
                'code' => 300,
                'msg' => '解析成功',
                'data' => [
                    'author' => $json['note']['noteDetailMap'][$id]['note']['user']['nickname'],
                    'uid' => $json['note']['noteDetailMap'][$id]['note']['user']['userId'],
                    'avatar'=>$json['note']['noteDetailMap'][$id]['note']['user']['avatar'],
                    'cover'=>$ch[0],
                    'like' => $json['note']['noteDetailMap'][$id]['note']['interactInfo']['likedCount'],
                    'title' =>  $json['note']['noteDetailMap'][$id]['note']['title'],
                    'images_num' => $images_num,
                    "images" => $ch
                    ]
                );
        }else{
            $arr = array(
                'code' => 200,
                'msg' => '解析成功',
                'data' => [
                    'author' => $json['note']['noteDetailMap'][$id]['note']['user']['nickname'],
                    'uid' => $json['note']['noteDetailMap'][$id]['note']['user']['userId'],
                    'avatar'=>$json['note']['noteDetailMap'][$id]['note']['user']['avatar'],
                    'cover' => $ch[0],
                    'like' => $json['note']['noteDetailMap'][$id]['note']['interactInfo']['likedCount'],
                    'title' =>  $json['note']['noteDetailMap'][$id]['note']['desc'],
                    'url' => 'https://sns-video-bd.xhscdn.com/' . $json['note']['noteDetailMap'][$id]['note']['video']['consumer']['originVideoKey'],
                    ]
                );
        }
        return $arr;
    }
    private function get_randip() {
        $ip_long = array(
            array('607649792', '608174079'), //36.56.0.0-36.63.255.255
            array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255
            array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255
            array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255
            array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255
            array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255
            array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255
            array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255
            array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255
            array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255
        );
        $rand_key = mt_rand(0, 9);
        $ip = long2ip(mt_rand(
            $ip_long[$rand_key][0],
            $ip_long[$rand_key][1]
            ));
        return $ip;
    }
    private function curl_general($url, $header) {
        $ch = curl_init() ;
        curl_setopt_array($ch, array(
           CURLOPT_URL => $url,
           CURLOPT_RETURNTRANSFER => true,
           CURLOPT_ENCODING => '',
           CURLOPT_BINARYTRANSFER=>true,
           CURLOPT_MAXREDIRS => 100,
           CURLOPT_TIMEOUT => 0,
           CURLOPT_FOLLOWLOCATION => true,
           CURLOPT_SSL_VERIFYHOST=>false,
           CURLOPT_SSL_VERIFYPEER => false,
           CURLOPT_HTTPHEADER => $header
        ));
        //$meta = curl_getinfo($ch);//print_r($meta);
        $output = curl_exec($ch);//print_r($output);
        curl_close ($ch);
        return $output;
    }
}

二、實現(xiàn)方法
1、id提取
www.xiaohongshu.com 瀏覽器直接復(fù)制過來的
xhslink.com 分享的短鏈地址
首先拿到文章的id,直接復(fù)制過來的從鏈接中提取,短鏈接從 header 頭中提取,或者重定向提取

這個id后面提取json中還會用到

拼接小紅書視頻/圖集地址:https://www.xiaohongshu.com/discovery/item/{小紅書id}

2、請求地址
請求這個地址,header 中包含 cookie 和 Accept ,這兩個請求參數(shù)登錄小紅書網(wǎng)頁版直接獲取。

請求中的內(nèi)容包含 json 格式文本,正則提取 json 格式文本,json 文本位于 window.INITIAL_STATE= 后

正則表達式如下:/window.__INITIAL_STATE__.+redmojiMap\"\:{}}}}/ ,php示例如下:

preg_match('/window\.__INITIAL_STATE__.+redmojiMap\"\:\{\}\}\}\}/',$html,$arr);
$arr = str_replace('window.__INITIAL_STATE__=' , "", $arr[0]);
$json = json_decode(str_replace('undefined' ,'null', $arr),true);

3、獲取圖集地址
圖集地址位于json文件中下面路徑,id為第一步獲取到的id,默認解析到的為無水印的地址,通過for循環(huán)存進數(shù)組。

$json['note']['noteDetailMap'][$id]['note']['imageList']

注意:請求頭中 Accept 中需包含 image/webp; 參數(shù),沒有這個參數(shù)請求的圖片包含水印,建議直接復(fù)制小紅書網(wǎng)頁中的 Accept 。

#示例代碼
$images_num=count($json['note']['noteDetailMap'][$id]['note']['imageList']);
    for($i = 0; $i < $images_num; $i++){
        $ch[$i] = $json['note']['noteDetailMap'][$id]['note']['imageList'][$i]['infoList'][1]['url'];
    }

4、獲取視頻地址
通過拼接 originVideoKey 參數(shù)得到無水印的地址

originVideoKey 參數(shù)位于:$json['note']['noteDetailMap'][$id]['note']['video']['consumer']['originVideoKey']

拼接方法:'

#示例代碼
 $arr = array(
            'code' => 200,
            'msg' => '解析成功',
            'data' => [
                'author' => $json['note']['noteDetailMap'][$id]['note']['user']['nickname'],
                'uid' => $json['note']['noteDetailMap'][$id]['note']['user']['userId'],
                'avatar'=>$json['note']['noteDetailMap'][$id]['note']['user']['avatar'],
                'cover' => $ch[0],
                'like' => $json['note']['noteDetailMap'][$id]['note']['interactInfo']['likedCount'],
                'title' =>  $json['note']['noteDetailMap'][$id]['note']['desc'],
                'url' => 'https://sns-video-bd.xhscdn.com/' . $json['note']['noteDetailMap'][$id]['note']['video']['consumer']['originVideoKey'],
                ]
            );
發(fā)帖
請先 登錄 再評論

? 2025挨踢網(wǎng) - 大家的導航! - iT200.COM 粵ICP備15047905號-9

主站蜘蛛池模板: 免费高清av一区二区三区| 久久久久久人妻一区精品| 国产一区二区三区国产精品| 天天综合色一区二区三区| 国产天堂在线一区二区三区| 日韩精品一区二区三区中文字幕 | 杨幂AV污网站在线一区二区| 乱色精品无码一区二区国产盗| 国产一区二区免费视频| 亚洲AV综合色区无码一区爱AV | 日本免费一区二区三区| 一区二区三区无码被窝影院| 视频一区二区三区在线观看| 久久综合精品国产一区二区三区| 亚洲av午夜福利精品一区 | 国产天堂在线一区二区三区 | 国产99视频精品一区| 国产成人av一区二区三区在线 | 无码av免费一区二区三区试看| 国产一区二区不卡老阿姨| 成人区人妻精品一区二区不卡视频 | 国产成人精品一区二三区熟女| 538国产精品一区二区在线| 精品无码一区二区三区爱欲| 精品不卡一区中文字幕| 久久久无码一区二区三区| 国产一区二区三区电影| 亚洲高清日韩精品第一区| 日本人真淫视频一区二区三区| 乱子伦一区二区三区| 亚洲成av人片一区二区三区 | 中文字幕VA一区二区三区| 人妻精品无码一区二区三区| 中文字幕精品一区二区精品| 日韩AV片无码一区二区不卡 | 国产精品制服丝袜一区| 精品一区二区三区中文字幕| 无码人妻精品一区二区蜜桃百度 | 国内精品一区二区三区在线观看 | 成人在线视频一区| 色欲AV蜜桃一区二区三|