栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Acwing活动打卡做题数统计爬虫

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Acwing活动打卡做题数统计爬虫

ACM老年人退役了。
感谢Acwing算法全家桶,让我的算法水平提升了不少。
之前就看到有些统计做题数的爬虫,但是通常只有hdu,cf,vj之类的爬虫,没看到有acwing活动打卡页面的。刚好期末大作业准备做一个能统计各大oj做题数的功能,于是在网上查找了些资料,写了这个爬虫。

用的语言是java,可以统计活动打卡5页的做题数(y总现在也只有3页),需要更多可以在for循环那里修改。
食用方法:在main函数里填入自己的id然后运行即可(id在右上角我的空间,上面的url有一串数字就是id)
不是专业搞爬虫的,仅供参考。

package acm.crawler;

import io.swagger.models.auth.In;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.cookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;


public class HttpClientDownPage {
    //设置代理,模范浏览器
    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";
    public static String sendGet(String url){
        //1.生成httpclient,相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom().setcookieSpec(cookieSpecs.STANDARD).setSocketTimeout(2000).setConnectTimeout(2000).build();
        CloseableHttpResponse response = null;
        StringBuilder html = new StringBuilder();
        //2.创建get请求,相当于在浏览器地址栏输入 网址
        try {
            for(int i=1;i<=5;i++)
            {
                HttpGet request = new HttpGet(url+"/"+i);
                request.setHeader("User-Agent",USER_AGENT);
                request.setConfig(requestConfig);
                //3.执行get请求,相当于在输入地址栏后敲回车键
                response = httpClient.execute(request);
                //4.判断响应状态为200,进行处理
                if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    //5.获取响应内容
                    HttpEntity httpEntity = response.getEntity();
                    html.append(EntityUtils.toString(httpEntity, "GBK"));
                } else {
                    //如果返回状态不是200,比如404(页面不存在)等,根据情况做处理,这里略
                    System.out.println("返回状态不是200");
                    System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
                }
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //6.关闭
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }
        return html.toString();
    }
    private static Integer paraseList(document document){
        //根据网页标签解析源码
        Elements elements = document.select("span[style=color: #6a737c;]");
        Integer result = 0;
        for(Element element:elements){
            String[] msg = element.text().split(" ");
            result += Integer.valueOf(msg[1]);
        }
        return result;
    }
    public static Integer getAcwing(String id)
    {
        String html = sendGet("https://www.acwing.com/user/myspace/activity/"+id);
        return paraseList(Jsoup.parse(html));
    }
     public static void main(String[] args)
    {
        System.out.println(getAcwing("1"));//输入你的id
    }
}

知道maven的可以直接导入依赖,就不用弄一堆jar包了

        
            org.jsoup
            jsoup
            1.11.3
        
                
            org.apache.httpcomponents
            httpclient
            4.5.9
        
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649566.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号