public class 代码分析 { public static void main(String[] args) throws Exception {
Matcher ma;
String caricatureUrl, caricatureName, html, picUrl, temp, temp1;
StringBuffer sb;
Pattern pa;
int book_1, book_2, i, j;
System.out.println("分析地址是:http://www.kkkmh.com/manhua/0503/jie-jie-shi.html");
html = getOneHtml("http://www.kkkmh.com/manhua/0503/jie-jie-shi.html");
// <li><a href="/manhua/0503/151/3051.html" title="结界师 第1本" target="_blank">第1本</a></li>
pa = Pattern.compile("<li><a href=\"/manhua/(.*?) title=(.*?)target=\"_blank\">(.*?)</a></li>", Pattern.DOTALL);
ma = pa.matcher(html);
while (ma.find()) {
temp1 = ma.group();
temp = "target=\"_blank\">";
book_2 = temp.length();
book_1 = temp1.indexOf(temp) + temp.length();
book_2 = temp1.indexOf("<", book_1);
caricatureName = temp1.substring(book_1, book_2);
temp = "<li><a href=\"";
book_1 = temp.length();
book_2 = temp1.indexOf("\"", book_1);
caricatureUrl = "http://www.kkkmh.com" + temp1.substring(book_1, book_2);
System.out.println("[" + caricatureName + "]" + caricatureUrl);
}
System.out.println("分析地址是:http://www.kkkmh.com/manhua/0503/151/29376.html");
html = getOneHtml("http://www.kkkmh.com/manhua/0503/151/29376.html");
// pic[0] = '2f636f6d696364617465322f636f6d6963616263642f652d682f6a6a732f7462702f3030316c7763796b62632e706e67';
pa = Pattern.compile("pic\\W(\\d.*?)\\W(.*?);", Pattern.DOTALL);
ma = pa.matcher(html);
while (ma.find()) {
System.out.println(ma.group());
temp = ma.group();
sb = new StringBuffer(temp.substring(temp.indexOf("'")).replaceAll("'", "").replaceAll(";", "").trim());
j = sb.length() / 2;
for (i = 0; i < j; i++) {
sb.insert(i * 3, "%");
}
picUrl = URLDecoder.decode(sb.toString(), "UTF-8");
System.out.println("http://mhc1.kkkmh.com" + picUrl);
}
}
private static String getOneHtml(String htmlurl) throws Exception {
URL url;
String temp;
StringBuffer sb = new StringBuffer();
url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
while ((temp = in.readLine()) != null) {
sb.append(temp);
}
in.close();
return sb.toString();
}
}
我要简化的就是代码转换问题,我现在用循环解决,还有更好的办法吗?
for (i = 0; i < j; i++) {
sb.insert(i * 3, "%");
}
picUrl = URLDecoder.decode(sb.toString(), "UTF-8");
现在就是用循环每隔两个字符+一个%,再转回正确地址.
Matcher ma;
String caricatureUrl, caricatureName, html, picUrl, temp, temp1;
StringBuffer sb;
Pattern pa;
int book_1, book_2, i, j;
System.out.println("分析地址是:http://www.kkkmh.com/manhua/0503/jie-jie-shi.html");
html = getOneHtml("http://www.kkkmh.com/manhua/0503/jie-jie-shi.html");
// <li><a href="/manhua/0503/151/3051.html" title="结界师 第1本" target="_blank">第1本</a></li>
pa = Pattern.compile("<li><a href=\"/manhua/(.*?) title=(.*?)target=\"_blank\">(.*?)</a></li>", Pattern.DOTALL);
ma = pa.matcher(html);
while (ma.find()) {
temp1 = ma.group();
temp = "target=\"_blank\">";
book_2 = temp.length();
book_1 = temp1.indexOf(temp) + temp.length();
book_2 = temp1.indexOf("<", book_1);
caricatureName = temp1.substring(book_1, book_2);
temp = "<li><a href=\"";
book_1 = temp.length();
book_2 = temp1.indexOf("\"", book_1);
caricatureUrl = "http://www.kkkmh.com" + temp1.substring(book_1, book_2);
System.out.println("[" + caricatureName + "]" + caricatureUrl);
}
System.out.println("分析地址是:http://www.kkkmh.com/manhua/0503/151/29376.html");
html = getOneHtml("http://www.kkkmh.com/manhua/0503/151/29376.html");
// pic[0] = '2f636f6d696364617465322f636f6d6963616263642f652d682f6a6a732f7462702f3030316c7763796b62632e706e67';
pa = Pattern.compile("pic\\W(\\d.*?)\\W(.*?);", Pattern.DOTALL);
ma = pa.matcher(html);
while (ma.find()) {
System.out.println(ma.group());
temp = ma.group();
sb = new StringBuffer(temp.substring(temp.indexOf("'")).replaceAll("'", "").replaceAll(";", "").trim());
j = sb.length() / 2;
for (i = 0; i < j; i++) {
sb.insert(i * 3, "%");
}
picUrl = URLDecoder.decode(sb.toString(), "UTF-8");
System.out.println("http://mhc1.kkkmh.com" + picUrl);
}
}
private static String getOneHtml(String htmlurl) throws Exception {
URL url;
String temp;
StringBuffer sb = new StringBuffer();
url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
while ((temp = in.readLine()) != null) {
sb.append(temp);
}
in.close();
return sb.toString();
}
}
我要简化的就是代码转换问题,我现在用循环解决,还有更好的办法吗?
for (i = 0; i < j; i++) {
sb.insert(i * 3, "%");
}
picUrl = URLDecoder.decode(sb.toString(), "UTF-8");
现在就是用循环每隔两个字符+一个%,再转回正确地址.
解决方案 »
- 用JAVA,在文本框里,保存应用程序上次关闭时的设定的值,怎么设定?
- 拜托各位帮个忙改个程序!!!!!!!!!!!!!!!!!!!!!!谢谢了 十万火急!!!!!!!(1)
- c++ java 运算符
- 如何在JBuider9中编写一个将word文件转换成PDF文件的应用程序。
- String = "1/3 "转换成double 或 float 型 为何有问题
- 关于j2re 1.4.2,只在客户端部署,会有哪些改善?
- CSS,求赐教!!!
- 好文章大家看(转贴)从JVM内存管理的角度谈谈静态方法和静态属性
- 为什么我这个applet 程序执行出错!
- 请问高手按键问题?
- 请教Java基础问题
- 表单提交上的数据不能插入数据库
StringBuffer ss = new StringBuffer();
for (int i = 0; i < sb.length(); i++) {
if (i % 2 == 0) {
ss.append("%").append(sb.substring(i, i + 2));
}
}