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");
}
}
GoogleLogoUtil
最新推荐文章于 2024-08-30 22:10:12 发布