爬虫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的排 ...
随机推荐
- python基础学习笔记(三)
序列概览 Python 包含6 种内建的序列,这里重点讨论最常用的两种类型:列表和元组. 列表与元组的主要区别在于,列表可以修改,元组则不能.也就是说如果要根据要求来添加元素,那么列表可以会更好用:而 ...
- python-小知识点-14
''' python2 python3 ''' #python2 print() print 'abc' range() xrange() 生成器 raw_input() #python3 print ...
- ELF文件格式分析
一般的 ELF 文件包括三个索引表:ELF header,Program header table,Section header table. 1)ELF header:在文件的开始,保存了路 ...
- 我的github地址 https://github.com/1010de/Test.git
构建之法老师叫交下任务学习github,经过一段时间的学习和了解,看介绍.看视频.看博客.初步认识到github的方便与好处. 自己试着去注册和使用github,已经慢慢学会了一些基本操作. ...
- MySQLi面向对象实践--insert、update、delete
执行insert <?php $mysqli = new Mysqli(); $mysqli->connect("localhost","root" ...
- CI框架在辅助函数中使用配置文件中的变量
问题: 现有一个自定义的辅助函数,想要获取配置文件中的配置项(配置文件路径为application/config/config.php) 分析: 辅助函数并不是定义在一个class中,而是很多个可供外 ...
- Java Heap Dump On OutOfMemoryError
-XX:+HeapDumpOnOutOfMemoryError Batch "C:\Program Files\Java\jdk1.8.0_162\bin\java.exe" -X ...
- Appium学习笔记4_元素定位方法
Appium之元素定位,如果对Android上如何使用工具获取页面元素有问题的,请转战到这:http://www.cnblogs.com/taoSir/p/4816382.html. 下面主要是针对自 ...
- mysql复杂查询
所谓复杂查询,指涉及多个表.具有嵌套等复杂结构的查询.这里简要介绍典型的几种复杂查询格式. 一.连接查询 连接是区别关系与非关系系统的最重要的标志.通过连接运算符可以实现多个表查询.连接查询主要包括内 ...
- error launching installer-最新版Win 10 解决方案
error 提示 error launching installer遇到 error的背景 楼主最近重新装了Windows 10 pro 64 bit 版,安装的时候选的地区是United State ...