google 翻译

url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=";
long start = System.currentTimeMillis();
Map<String, String> header = new HashMap<>();
header.put("User-Agent",
		"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");

JSONArray jsonArray = httpUtil.get(googleTranslateUrl + "香港", header);
log.info("*****************jsonArray is {}", jsonArray);
JSONArray retStr = jsonArray.getJSONArray(0).getJSONArray(0).getJSONArray(0);
log.info("***retStr is {}", retStr);
String cityName = retStr.getString(0);
log.info("***cityName is {}", cityName);
log.info("cost time is {}",(System.currentTimeMillis()-start));

请求后的json 结果:

[
    [
        [
            "Hong Kong",
            "香港",
            null,
            null,
            2
        ]
    ],
    null,
    "zh-CN",
    null,
    null,
    null,
    null,
    null,
    [
        [
            "zh-CN"
        ],
        null,
        [
            0.9609375
        ],
        [
            "zh-CN"
        ]
    ]
]

一段时间内大量请求的话就需要使用代理来发送请求,避免被和谐(反爬虫机制)

原文地址:https://www.cnblogs.com/wanthune/p/11410702.html