Skip to content

Instantly share code, notes, and snippets.

@wangpin34
Created May 5, 2017 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangpin34/2562eadda9534be4a1b05c47b7255f9d to your computer and use it in GitHub Desktop.
Save wangpin34/2562eadda9534be4a1b05c47b7255f9d to your computer and use it in GitHub Desktop.
Java baidu image
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Query {
public static String urlencode(String url){
String result = null;
try
{
result = URLEncoder.encode(url, "UTF-8")
.replaceAll("\\+", "%20")
.replaceAll("\\%21", "!")
.replaceAll("\\%27", "'")
.replaceAll("\\%28", "(")
.replaceAll("\\%29", ")")
.replaceAll("\\%7E", "~");
}
// This exception should never occur.
catch (UnsupportedEncodingException e)
{
result = url;
}
return result;
}
public static String parseURL(String objURL){
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("_z2C$q", ":");
map1.put("AzdH3F", "/");
map1.put("_z&e3B", ".");
HashMap<String,String> map2 = new HashMap<String, String>();
map2.put("w", "a");
map2.put("k", "b");
map2.put("v", "c");
map2.put("1", "d");
map2.put("j", "e");
map2.put("u", "f");
map2.put("2", "g");
map2.put("i", "h");
map2.put("t", "i");
map2.put("3", "j");
map2.put("h", "k");
map2.put("s", "l");
map2.put("4", "m");
map2.put("g", "n");
map2.put("5", "o");
map2.put("r", "p");
map2.put("q", "q");
map2.put("6", "r");
map2.put("f", "s");
map2.put("p", "t");
map2.put("7", "u");
map2.put("e", "v");
map2.put("o", "w");
map2.put("8", "1");
map2.put("d", "2");
map2.put("n", "3");
map2.put("9", "4");
map2.put("c", "5");
map2.put("m", "6");
map2.put("0", "7");
map2.put("b", "8");
map2.put("l", "9");
map2.put("a", "0");
for(Map.Entry<String, String> entry : map1.entrySet()){
objURL = objURL.replace(entry.getKey(), entry.getValue());
}
String[] array = objURL.split("");
StringBuffer result = new StringBuffer();
for(String e : array){
if(map2.get(e) != null){
e = map2.get(e);
}
result.append(e);
}
return result.toString();
}
public static void main(String[] args){
StringBuffer result = new StringBuffer();
String key = urlencode("娜美");
String host = "http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%s&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=%s&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn=%s&rn=%s&gsm=1e&1493955383425=";
host = String.format(host, key, key, 0, 100);
System.out.println(host);
try {
URL realUrl = new URL(host);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("web-proxy.austin.hp.com", 8080));
URLConnection connection = realUrl.openConnection(proxy);
connection.setRequestProperty("host", "image.baidu.com");
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36");
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String resultStr = result.toString();
System.out.println(resultStr);
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(resultStr);
JSONObject jsonObject = (JSONObject) obj;
JSONArray data = (JSONArray)jsonObject.get("data");
System.out.println(String.format("共有 %s 条记录", data.size()));
Iterator<JSONObject> iterator = data.iterator();
int num = 0;
while (iterator.hasNext()) {
JSONObject item = iterator.next();
String objURL = (String)item.get("objURL");
if(objURL != null){
String realURL = parseURL(objURL);
System.out.println(++num + ":" + realURL);
}
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment