日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

爬有道翻译的几种方法

發(fā)布時(shí)間:2025/1/21 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬有道翻译的几种方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

方法一:

剛開始用selenium來爬

public static String changeArticle(String srcTxt) {WebDriver driver = SeleniumUtil.getNoHeadDriver();driver.get("http://fanyi.youdao.com/");try {driver.findElement(By.id("inputOriginal")).sendKeys(srcTxt);String result = driver.findElement(By.id("transTarget")).getAttribute("innerHTML");while (result == null || "".equals(result)) {Thread.sleep(100);result = driver.findElement(By.id("transTarget")).getAttribute("innerHTML");}result = HtmlUtil.changeP(result);result=StringUtil.kill(result, "<", ">");SeleniumUtil.click(driver, driver.findElement(By.id("inputDelete")));driver.findElement(By.id("inputOriginal")).sendKeys(result);Thread.sleep(100);result = driver.findElement(By.id("transTarget")).getAttribute("innerHTML");while (result == null || "".equals(result)) {Thread.sleep(100);result = driver.findElement(By.id("transTarget")).getAttribute("innerHTML");}result = HtmlUtil.changeP(result);result=StringUtil.kill(result, "<", ">");return result;} catch (InterruptedException e) {e.printStackTrace();} finally {driver.close();driver.quit();}return "";}

效率太低,放棄了

方法二:

看了網(wǎng)上的,注釋掉salt、sign

fanyi.youdao.com/translate_o改成了fanyi.youdao.com/translate

英文轉(zhuǎn)中文沒問題,中文轉(zhuǎn)英文,返回的就是亂碼,我一度以為是我的post方法導(dǎo)致的,折騰了一整夜,放棄了

public static String translate_en2zh(Map<String, String> params,String i,String from,String to) throws Exception {//這個只能英文轉(zhuǎn)中文Map<String,String> forms=new HashMap<String,String>();forms.put("i", i);forms.put("from",from);//en zh-CHS viforms.put("to", to);forms.put("smartresult", "dict");forms.put("client", "fanyideskweb"); // forms.put("salt", "16374128938108"); // forms.put("sign", "40869be25369a6f5a33a689596a4ffea");forms.put("lts", "1637412893810");forms.put("bv", "b0ff5d17f404993192085bf8b1e93587");forms.put("doctype", "json");forms.put("version", "2.1");forms.put("keyfrom", "fanyi.web");forms.put("action", "FY_BY_REALTlME");String post = SpiderUtil.post("https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule", params, forms);return post;}

方法三:

破解sign的奧秘

網(wǎng)上有現(xiàn)成的方法,但是給我返回了{(lán)"errorCode":50}

后來發(fā)現(xiàn)是sign的密鑰不同。自己寫了個getCode()方法,讓它自動獲取,總算沒問題了。

我后來想將越南語轉(zhuǎn)成英文,返回了{(lán)"errorCode":40}。再看了一下網(wǎng)站,發(fā)現(xiàn)都是中文轉(zhuǎn)其他,或者其他轉(zhuǎn)中文,沒有其他轉(zhuǎn)其他的操作。應(yīng)該是網(wǎng)站不支持這種轉(zhuǎn)換,不了了之。

public static String translate(Map<String, String> params,String i,String from,String to) throws Exception {params.clear();params.put("Cookie","OUTFOX_SEARCH_USER_ID=1799185238@10.169.0.83;");params.put("Referer","http://fanyi.youdao.com/");params.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");Map<String,String> forms=new HashMap<String,String>();String bv= DigestUtils.md5Hex("Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");String ts=String.valueOf(System.currentTimeMillis());String slat = String.valueOf(ts + (long) (Math.random() * 10 + 1));String sign = DigestUtils.md5Hex("fanyideskweb" + i + slat + getCode());forms.put("i", i);forms.put("from",from);//en zh-CHS viforms.put("to", to);forms.put("smartresult", "dict");forms.put("client", "fanyideskweb");forms.put("salt", slat);forms.put("sign", sign);forms.put("lts", ts);forms.put("bv", bv);forms.put("doctype", "json");forms.put("version", "2.1");forms.put("keyfrom", "fanyi.web");forms.put("action", "FY_BY_REALTlME");forms.put("typoResult", "true");String post = SpiderUtil.post("http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule", params, forms);return post;}private static String getCode() throws IOException{String downhref="";String page=SpiderUtil.getPageContent("http://fanyi.youdao.com/");for(String s:Hp.find(page, "script[attr=src]")){if(s.endsWith("fanyi.min.js")){downhref=s;break;}}String tem2="c:/temspace/tem"+System.currentTimeMillis()+".txt";SpiderUtil.download(downhref, tem2);String file = FileUtil.getFile(tem2);FileUtil.del(tem2);String feature = SearchUtil.getListWithFeature(file, Arrays.asList("sign:n.md5(\"","\")")).get(0);feature=feature.replace("sign:n.md5(\"fanyideskweb\"+e+i+\"", "").replace("\")", "");return feature;}

總結(jié)

以上是生活随笔為你收集整理的爬有道翻译的几种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。