个人小站

最新碎语:你好三月

您的位置:个人小站 >C> C语言使用cJSON解析B站视频分页数据

C语言使用cJSON解析B站视频分页数据

测试地址:https://api.bilibili.com/x/web-interface/newlist?rid=24&pn=1&ps=20


获取的数据:

QQ截图20210304100534.png



首先需要获取到json数据,使用curl就可以:

curl 请求头:


header = "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0 \
                      Accept-Encoding:gzip, deflate, br \
                      Referer:https://www.bilibili.com/ \
                      Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2 \
                      Content-Type:application/x-www-form-urlencoded;charset=UTF-8 \
                      Connection:keep-alive";



相关配置

struct curl_slist *pList = NULL;

pList = curl_slist_append(pList,header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, pList);

curl_easy_setopt (curl,CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt (curl,CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt (curl,CURLOPT_TIMEOUT, 60L);
curl_easy_setopt (curl,CURLOPT_CONNECTTIMEOUT, 10L);
curl_easy_setopt (curl,CURLOPT_ENCODING, "");


json解析代码:

//test address: https://api.bilibili.com/x/web-interface/newlist?rid=24&pn=1&ps=20
static void parse_json(char *json, spider_enum se)
{
    int archives_size, index;
    cJSON *cjson, *data_object, *object, *page_item, *archive_object, *archive_item;

    cjson = cJSON_Parse(json);

    if(NULL == cjson){
        printf("json pack into cjson error...");
    }
    else
    {
        //打包成功调用cJSON_Print打印输出
//        printf("%s\n", cJSON_Print(cjson));
//        file_write("tmp.txt", cJSON_Print(cjson));
        //获取data对象
        data_object = cJSON_GetObjectItem(cjson,"data");
        //获取page对象,分页数据
        object = cJSON_GetObjectItem(data_object,"page");
        page_item = cJSON_GetObjectItem(object, "count");
        page_item = cJSON_GetObjectItem(object, "num");
        page_item = cJSON_GetObjectItem(object, "size");
//        char buf[10];
//        sprintf(buf, "%f",page_item->valuedouble);
        //获取archives对象,视频列表数据
        object = cJSON_GetObjectItem(data_object, "archives");

        if(NULL != object)
        {
            archives_size = cJSON_GetArraySize(object);
            setxy(0, 0);
            file_write("tmp.txt", "", "w");
            for(index=0;index<archives_size;index++)
            {
                char buf[128];
                archive_object = cJSON_GetArrayItem(object, index);
                //获取标题
                archive_item = cJSON_GetObjectItem(archive_object, "title");
                sprintf(buf, "%s \n", archive_item->valuestring);
                file_write("tmp.txt", buf, "a");
                //获取时长
                archive_item = cJSON_GetObjectItem(archive_object, "duration");
                sprintf(buf, "%f \n", archive_item->valuedouble);
                file_write("tmp.txt", buf, "a");
                //获取bvid
                archive_item = cJSON_GetObjectItem(archive_object, "bvid");
                sprintf(buf, "%s \n", archive_item->valuestring);
                file_write("tmp.txt", buf, "a");
            }

        }

    }

    cJSON_Delete(cjson);

}


文件保存函数:

int file_write(char * name, char * str, char * type)
{
    FILE *fptr;

    fptr = fopen(name, type);
    if(fptr == NULL)
    {
      printf("Error!");
      return -1;
    }

    fprintf(fptr,"%s", str);
    fclose(fptr);

    return 0;
}

---

转载请注明本文标题和链接:《C语言使用cJSON解析B站视频分页数据

分享到:

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交