GoogleLogoUtil



package com.test;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GoogleLogoUtil {

public static void main(String[] args) throws Exception {
start();
}

public static void start() {
String div = "<div><div class=\"l_t\"></div><div class=\"l_c\"><img src=\"00_1/1281627409056.gif\" title=\"\"/></div></div>";
try {
String model = readerFile("D:\\docs\\code\\html\\dh189\\google.html", "GBK");
String js = readerFile("D:\\docs\\code\\html\\dh189\\json.html");
JSONArray array = new JSONArray(js);
for (int i = 0; i < array.length(); i++) {
Document doc = Jsoup.parse(model);
doc.getElementById("logos").html("");
Elements elements = doc.select("a[n]");
for (Element element : elements) {
element.attr("href", element.attr("n") + "_1.html");
}
JSONObject json = array.getJSONObject(i);
String key = json.keys().next().toString();
JSONArray arr = json.getJSONArray(key);
String[] nvs = key.split("_");
elements = doc.select("a[v]");
for (Element element : elements) {
element.attr("href", nvs[0] + "_" + element.attr("v") + ".html");
}
if (arr.length() > 0) {
StringBuffer sb = new StringBuffer();
for (int j = 0; j < arr.length(); j++) {
JSONObject jn = arr.getJSONObject(j);
Document d = Jsoup.parse(div);
String t = jn.getString("title_zh_cn") + " (" + jn.getString("title_en") + ")";
d.select(".l_t").get(0).html(t);
d.select("img").get(0).attr("src",
jn.getString("url")).attr("title",
t.replaceAll(" ", "")).attr("alt", t.replaceAll(" ", ""));
sb.append(d.body().html());
}
doc.getElementById("logos").html(sb.toString());
}else{
doc.getElementById("logos").html("没有数据");
}
String t1=doc.select("[n=" + nvs[0] + "]").get(0).addClass("sel").text();
String t2=doc.select("[v=" + nvs[1] + "]").get(0).addClass("sel").text();
doc.title("Google logo 大全("+t1+"年 "+t2+")--导航189");
writeFile("D:\\docs\\code\\html\\dh189\\"+key+".html", doc.toString());
}

} catch (Exception e) {
e.printStackTrace();
}
}

public static String readerFile(String dir, String... charset) throws IOException {
String ch = "UTF-8";
if (charset.length > 0) {
ch = charset[0];
}
return FileUtils.readFileToString(new File(dir), ch);
}

public static void writeFile(String dir, String data) throws IOException {
FileUtils.writeStringToFile(new File(dir), data,"GBK");
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值