• 统计自己洛谷AC题目爬虫


    update on 2018.9.23 洛谷开启了通过题目难度的显示,现在这个爬虫已经没用了,也就看看吧。

    本文中的代码转载于https://github.com/ACodreamer/Luogu-AC-Difficulty。

    要注意的就是爬的用户必须是没有开启完全隐私,否则就挂了

    #include <stdio.h>
    #include <windows.h>
    #include <conio.h>
    #ifdef URLDownloadToFile
    #undef URLDownloadToFile
    #endif
    typedef int(__stdcall *UDF)(LPVOID,LPCSTR,LPCSTR,DWORD,LPVOID);
    UDF URLDownloadToFile = (UDF)GetProcAddress(LoadLibrary("urlmon.dll"),"URLDownloadToFileA");
    char* strfind(char *text,char *temp)
    {
        int i = -1,j,l = strlen(temp);
        while(text[++i])
        {
            for(j=0;j<l;j++)if(text[i+j] == 0 || text[i+j] != temp[j])break;
            if(j == l)return text+i+l;
        }
        return 0;
    }
    void UTF8ToANSI(char *str)
    {
        int len = MultiByteToWideChar(CP_UTF8,0,str,-1,0,0);
        WCHAR *wsz = new WCHAR[len+1];
        len = MultiByteToWideChar(CP_UTF8,0,str,-1,wsz,len);
        wsz[len] = 0;
        len = WideCharToMultiByte(CP_ACP,0,wsz,-1,0,0,0,0);
        len = WideCharToMultiByte(CP_ACP,0,wsz,-1,str,len,0,0);
        str[len] = 0;
        delete []wsz;
    }
    HANDLE hOutput;
    char name[32];
    int count[8];
    void problem(char *str)
    {
        int i = 0,len;
        DWORD unused;
        char prob[32],url[128],*file,*ptr;
        HANDLE hFile;
        COORD pos = {0,2};
        while(*str != '<')prob[i++] = *str++;
        prob[i] = 0;
        sprintf(url,"https://www.luogu.org/problemnew/show/%s",prob);
        URLDownloadToFile(0,url,"download.tmp",0,0);
        hFile = CreateFile("download.tmp",GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
        len = GetFileSize(hFile,0);
        file = ptr = new char[len+3];
        ReadFile(hFile,file,len,&unused,0);
        file[len] = file[len+1] = 0;
        CloseHandle(hFile);
        file = strfind(file,"xE9x9AxBExE5xBAxA6");
        if(file == 0){delete []ptr;return;}
        file = strfind(file,"lg-bg-");
        if(file[0] == 'r')count[0]++;
        else if(file[0] == 'o')count[1]++;
        else if(file[0] == 'y')count[2]++;
        else if(file[0] == 'g' && file[2] == 'e')count[3]++;
        else if(file[0] == 'b' && file[4] == 'l')count[4]++;
        else if(file[0] == 'p')count[5]++;
        else if(file[0] == 'b' && file[4] == 'd')count[6]++;
        else if(file[0] == 'g' && file[2] == 'a')count[7]++;
        delete []ptr;
        SetConsoleCursorPosition(hOutput,pos);
        printf(
            "%s 的统计:    %s        
    "
            "入门难度:       %d
    "
            "普及-:          %d
    "
            "普及/提高-:     %d
    "
            "普及+/提高:     %d
    "
            "提高+/省选-:    %d
    "
            "省选/NOI-:      %d
    "
            "NOI/NOI+/CTSC:  %d
    "
            "尚无评定:       %d
    ",name,prob,count[0],count[1],count[2],count[3],
            count[4],count[5],count[6],count[7]);
    }
    int main()
    {
        int uid,len,i = 0;
        DWORD unused;
        char url[128],user[16],*file,*ptr;
        HANDLE hFile;
        hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        printf("请输入你的洛谷UID: ");
        scanf("%d",&uid);
        sprintf(url,"https://www.luogu.org/space/show?uid=%d",uid);
        URLDownloadToFile(0,url,"download.tmp",0,0);
        hFile = CreateFile("download.tmp",GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
        len = GetFileSize(hFile,0);
        file = new char[len+3];
        ReadFile(hFile,file,len,&unused,0);
        file[len] = file[len+1] = 0;
        CloseHandle(hFile);
        UTF8ToANSI(file);
        sprintf(user,"U%d ",uid);
        ptr = strfind(file,user);
        if(ptr)
        {
            while(ptr[0] != '<' || ptr[1] != '/' || ptr[2] != 'h')name[i++] = *ptr++;
            printf("
    %s 的统计: ",name);
            ptr = strfind(file,"通过题目</h2>
    [<");
            if(ptr)while(*ptr != '<')
            {
                ptr = strfind(ptr,"">");
                problem(ptr);
                ptr = strfind(ptr,"]
    ");
            }
            else printf("还没有通过任何题目
    ");
        }
        else printf("用户不存在
    ");
        DeleteFile("download.tmp");
        delete []file;
        printf("
    Press any key to exit. . .");
        getch();
        return 0;
    }
    转载是允许的,但是除了博主同意的情况下,必须在文章的明显区域说明出处,否则将会追究其法律责任。
  • 相关阅读:
    机器学习 -- 用户画像
    机器学习 -- 分类
    机器学习 -- 聚类
    机器学习 -- 回归
    数据分析 -- 流程
    Nginx + Tomcat7 + redis session一致性问题
    机器学习 -- 信息论
    机器学习 -- 统计与分布
    ElasticSearch 学习一: 基本命令
    问题17:如何将多个小字符串拼接成一个大的字符串
  • 原文地址:https://www.cnblogs.com/Xray-luogu/p/9652571.html
Copyright © 2020-2023  润新知