爬虫acm比赛成绩(多页成绩整合在一起、获取复制不了的数据)(hihocoder、计蒜客)
https://github.com/congmingyige/web-crawler_rank-of-competition-in-JiSuanKe-and-hihocoder
1. 计蒜客(获取复制不了的数据)
import java.util.Scanner; /**
* 无法从网页上获得源代码
*/ public class GetScore_jisuanke { static String PREFIX_UNICODE= "\\u";
static char ascii2Char(String str) {
if (str.length() != 6) {
throw new IllegalArgumentException("Ascii string of a native character must be 6 character.");
}
if (!PREFIX_UNICODE.equals(str.substring(0, 2))) {
throw new IllegalArgumentException("Ascii string of a native character must start with \"\\u\".");
}
String tmp = str.substring(2, 4); // 将十六进制转为十进制
int code = Integer.parseInt(tmp, 16) << 8; // 转为高位,后与地位相加
tmp = str.substring(4, 6);
code += Integer.parseInt(tmp, 16); // 与低8为相加
return (char) code;
} static String ascii2Native(String str) {
StringBuilder sb = new StringBuilder();
int begin = 0;
int index = str.indexOf(PREFIX_UNICODE);
while (index != -1) {
sb.append(str.substring(begin, index));
sb.append(ascii2Char(str.substring(index, index + 6)));
begin = index + 6; index = str.indexOf(PREFIX_UNICODE, begin);
}
sb.append(str.substring(begin));
return sb.toString();
} /*
* unicode代码 来自 黑暗的笑 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/xia744510124/article/details/51322107?utm_source=copy
*/ public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str,s;
int s1,s2,s3;
String tag=new String("</script>");
int x,y,sum_pro,i; while ((str=in.nextLine())!=null) {
if (str.length()>=9 && str.substring(0,9).equals(tag)) { s="problem_naming";
x=str.indexOf(s);
x+=s.length()+3;
y=str.indexOf("]",x);
sum_pro=(y-x)/4; System.out.print("team\tschool\tcount\ttime\t");
for (i=0;i<sum_pro;i++)
System.out.print((char)(65+i)+"\t");
System.out.println(); y=str.indexOf("prev_page_url",y); while (true) {
s="name";
x=str.indexOf(s,y);
if (x==-1)
break;
x+=s.length()+3;
y=str.indexOf("\"",x);
System.out.print(str.substring(x,y)+"\t"); s="school";
x=str.indexOf(s,y);
x+=s.length()+3;
y=str.indexOf("\"",x);
System.out.print(ascii2Native(str.substring(x,y))+"\t"); s="score";
x=str.indexOf(s,y);
x+=s.length()+2;
y=str.indexOf(",",x);
System.out.print(str.substring(x,y)+"\t"); s="cost";
x=str.indexOf(s,y);
x+=s.length()+2;
y=str.indexOf(",",x);
System.out.print(str.substring(x,y)+"\t"); // until not exists or ==cost -1
for (i=1;i<=sum_pro;i++) {
//cost":120,"exact_cost":7144,"submit_count":4,"problem_score":1,"score":0
s="cost\"";
x=str.indexOf(s,y);
x+=s.length()+1; //2-1
y=str.indexOf(",",x);
s1=Integer.valueOf(str.substring(x,y)); s="exact_cost";
x=str.indexOf(s,y);
x+=s.length()+2;
y=str.indexOf(",",x);
s2=Integer.valueOf(str.substring(x,y)); s="submit_count";
x=str.indexOf(s,y);
x+=s.length()+2;
y=str.indexOf(",",x);
s3=Integer.valueOf(str.substring(x,y)); if (s2!=0)
System.out.print(s1);
else
System.out.print("——");
System.out.print("("+s3+")\t");
}
System.out.println();
}
}
}
}
}
效果:

2. hihocoder(多页成绩整合在一起)
/**
* get source code:
* https://www.cnblogs.com/chaohu13/p/5337498.html
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL; public class GetScore_hiho {
public static void main(String args[]){
URL url;
int responsecode;
HttpURLConnection urlConnection;
BufferedReader reader;
String str,str1;
String tag=new String("<tr class=\"std-acm\">");
String website;
//修改1 必须是"rank?page="形式
website=new String("http://hihocoder.com/contest/acmicpc2018beijingonline/rank?page=1");
int x,y,i;
//修改2
int page=13;
int index=0;
Boolean vis; vis=false; //首栏只用存在一次
for (index=1;index<=page;index++) {
try{
//生成一个URL对象,要获取源代码的网页地址为:http://www.sina.com.cn
x=website.indexOf("=");
website=website.substring(0,x+1)+String.valueOf(index);
url=new URL(website); //打开URL
urlConnection = (HttpURLConnection)url.openConnection();
//获取服务器响应代码
responsecode=urlConnection.getResponseCode();
if(responsecode==200){
//得到输入流,即获得了网页的内容
reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));//GBK
while((str=reader.readLine().trim())!=null){
// System.out.println(str); //test
if (str.equals(tag)==true) {
str=reader.readLine().trim();
x=str.indexOf(">");
y=str.indexOf("<",x); if (str.substring(x+1,y).equals("Rank")==true) {
if (vis==false) {
vis=true;
System.out.print(str.substring(x+1,y).trim()+"\t");
while (true) {
str=reader.readLine().trim();
if (str.equals("</tr>")==true)
break;
x=str.indexOf(">");
y=str.indexOf("<",x);
System.out.print(str.substring(x+1,y).trim()+"\t"); if ((x=str.indexOf(">",y))!=str.length()-1) {
y=str.indexOf("<",x);
System.out.print(str.substring(x+1,y).trim()+"\t");
}
}
System.out.println();
}
// System.exit(0); //test
}
else {
/*
* <td>1</td>
* <td>清华大学</td>
*/
System.out.print(str.substring(x+1,y).trim()+"\t");
for (i=2;i<=2;i++) { //1+1
str=reader.readLine().trim();
x=str.indexOf(">");
y=str.indexOf("<",x);
System.out.print(str.substring(x+1,y).trim()+"\t");
} //<td><a class="fn-ell" style="display: block;" href="/user/109506">team181814</a></td>
str=reader.readLine().trim();
x=str.indexOf(">",5);
y=str.indexOf("<",x);
System.out.print(str.substring(x+1,y).trim()+"\t"); /*
* <td class="solved">8</td>
* <td>15:20:09</td>
*/
for (i=1;i<=2;i++) {
str=reader.readLine().trim();
x=str.indexOf(">");
y=str.indexOf("<",x);
System.out.print(str.substring(x+1,y).trim()+"\t");
} while (true) {
str=reader.readLine().trim();
if (str.equals("</tr>")==true)
break;
str=reader.readLine().trim();
str=reader.readLine().trim();
if (str.equals("</td>")==true)
str="";
else if (str.charAt(0)>='0' && str.charAt(0)<='9') {
x=str.indexOf("<br>");
if (x!=-1) {
y=str.indexOf(")",x+4);
str=str.substring(0,7)+" "+str.substring(x+4,y+1);
str1=reader.readLine(); //读多一行
}
else
str=str.substring(0,7);
}
else {
x=str.indexOf(")");
str=str.substring(0,x+1);
str1=reader.readLine(); //读多一行
}
System.out.print(str+"\t");
}
System.out.println();
}
// System.exit(0); //test
}
}
}
else{
System.out.println("获取不到网页的源码,服务器响应代码为:"+responsecode);
}
}
catch(Exception e){
//End Of Input
// System.out.println("获取不到网页的源码,出现异常:"+e);
}
} }
}
/*
p=Pattern.compile("<td>|</td>");
m=p.matcher(str);
str=m.replaceAll("");
System.out.print(str+"\t");
*/
效果:

爬虫acm比赛成绩(多页成绩整合在一起、获取复制不了的数据)(hihocoder、计蒜客)的更多相关文章
- 计蒜客模拟赛5 D2T1 成绩统计
又到了一年一度的新生入学季了,清华和北大的计算机系同学都参加了同一场开学考试(因为两校兄弟情谊深厚嘛,来一场联考还是很正常的). 不幸的是,正当老师要统计大家的成绩时,世界上的所有计算机全部瘫痪了. ...
- 计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)
ACM训练联盟周赛 这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树, 先立个flag,BZOJ3065: 带插入区间K小值 计蒜客 Zeratul与Xor ...
- 计蒜客 成绩统计 (Hash表)
链接 : Here! 思路 : 如果用 $STL$ 的 $map$ 或者是使用 $unordered\underline{}map$ 的话是会 $T$ 的, 所以得手写一个 $hash表$. 其实这个 ...
- 计蒜客 ACM训练联盟周赛 第一场 从零开始的神棍之路 暴力dfs
题目描述 ggwdwsbs最近被Zeratul和Kyurem拉入了日本麻将的坑.现在,ggwdwsbs有13张牌,Kyurem又打了一张,加起来有14张牌.ggwdwsbs想拜托你帮他判断一下,这14 ...
- 计蒜客 ACM竞赛高校联盟训练赛 第8场 煎牛排
水一水. https://nanti.jisuanke.com/t/24205 煎牛排 题目描述 又是一个难得的周六,是时候远离食堂和外卖出去大吃一顿了.圈内知名吃货AA正想着中午去吃汉堡炸鸡烤肉火锅 ...
- 计蒜客 ACM训练联盟周赛 第一场 Christina式方格取数 思维
助手Christina发明了一种方格取数的新玩法:在n*m的方格棋盘里,每个格子里写一个数.两个人轮流给格子染色,直到所有格子都染了色.在所有格子染色完后,计算双方的分数.对于任意两个相邻(即有公共边 ...
- 计蒜客 ACM训练联盟周赛 第一场 Alice和Bob的Nim游戏 矩阵快速幂
题目描述 众所周知,Alice和Bob非常喜欢博弈,而且Alice永远是先手,Bob永远是后手. Alice和Bob面前有3堆石子,Alice和Bob每次轮流拿某堆石子中的若干个石子(不可以是0个), ...
- ACM学习历程—计蒜客15 单独的数字(位运算)
http://nanti.jisuanke.com/t/15 题目要求是求出只出现一次的数字,其余数字均出现三次. 之前有过一个题是其余数字出现两次,那么就是全部亦或起来就得到答案. 这题有些不太一样 ...
- 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)
M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...
随机推荐
- sqli-labs less 5-6
sqli-labs less 5-6 从源代码中可以看到,运行返回结果正确的时候只返回you are in....,不会返回数据库当中的信息了,以前的union联合查询就不能用了,开始尝试盲注. 简单 ...
- Telephone Phrases
There are some common phrases and sentences you can use when speaking on the telephone. The informal ...
- 2019年以后ArcGIS 调用天地图的资源URL
2019年1月1日起,天地图做出如下变更,导致直接在Arcgis/ArcMap中添加WMTS服务不能用了. 国家天地图解释的很清楚,注册个人用户就可以了. 原有调用方式不变,只要在URL 后添加“&a ...
- 定时备份windows机器上的文件到linux服务器上的操作梳理(rsync)
由于需要对网络设备做备份,备份文件是放到windows机器上的.现在需要将备份数据同步到linux备份机器上,想到的方案有三种: 1)将windows的备份目录共享出来,然后在linux服务器上进行挂 ...
- let命令和块级作用域
学习了阮一峰老师的ES6,http://es6.ruanyifeng.com/,收益良多. 一.let命令1.概念:let命令用于声明变量,和var类似,但是使用let命令所声明的变量只有在该变量所在 ...
- 爬虫时http错误提示
在爬虫,请求网站的时候,有时候出现域名报错,所出现的代码所对应的意思:
- Maven入门指南④:仓库
1 . 仓库简介 没有 Maven 时,项目用到的 .jar 文件通常需要拷贝到 /lib 目录,项目多了,拷贝的文件副本就多了,占用磁盘空间,且难于管理.Maven 使用一个称之为仓库的目录,根据构 ...
- 微软开源的Trill是什么?
微软开源的Trill是什么? https://www.cnblogs.com/CattaC/p/10143445.html 微软开源的Trill是什么? 以下是一篇15年的文章的译文:https:// ...
- PAT 甲级 1045 Favorite Color Stripe
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...
- [转帖]Linux内核为大规模支持100Gb/s网卡准备好了吗?并没有
Linux内核为大规模支持100Gb/s网卡准备好了吗?并没有 之前用 千兆的机器 下载速度 一般只能到 50MB 左右 没法更高 万兆的话 可能也就是 200MB左右的速度 很难更高 不知道后续的服 ...