歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java和Python使用有道詞典制作查單詞腳本

Java和Python使用有道詞典制作查單詞腳本

日期:2017/3/1 9:27:02   编辑:Linux編程

先上兩張圖看一下效果
Java的:

Python的:

今天突發奇想,想做個查單詞的東西,就趕緊去有道詞典官網看了一下,原來我們要查詢的單詞是嵌入在網頁地址中送給有道詞典的,然後頁面的結果就是我們需要的單詞釋義,所以這個東西需要的技術知識只有:

正則表達式

我們要做的只是從獲取到的網頁源碼中提取處單詞釋義,所以這裡只說提取單詞釋義的正則表達式。
分析網頁源碼,我們可以看到,單詞釋義都在一個div標簽內,如圖:

首要目標是獲取這一部分,正則表達式可以這樣寫:

(?s)<div class=\"trans-container\">.*?<ul>.*?</div>
//(?s)的含義是使'.'可以匹配換行符,默認是不匹配的
//.*?意思是在非貪婪模式下,匹配任意多個字符獲取到這一部分後,進一步的,我們需要的是裡面的單詞釋義,所以,我們可以這樣:

(?m)<li>(.*?)</li>
//(?m)的含義是按行匹配,在沒一行都按照這個正則表達式匹配,默認情況是不分行,統一匹配的
//這裡用小括號把.*?包起來,為的是可以直接獲取單詞的含義,捨去旁邊的標簽下面是具體的代碼:

一,Java代碼
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();

System.out.print("請輸入你要查的單詞:");
Scanner s = new Scanner(System.in);
String word = s.nextLine();
word = word.replaceAll(" ","+");

//根據查找單詞構造查找地址
HttpGet getWordMean = new HttpGet("http://dict.youdao.com/search?q=" + word + "&keyfrom=dict.index");
CloseableHttpResponse response = httpClient.execute(getWordMean);//取得返回的網頁源碼

String result = EntityUtils.toString(response.getEntity());
response.close();
//注意(?s),意思是讓'.'匹配換行符,默認情況下不匹配
Pattern searchMeanPattern = Pattern.compile("(?s)<div class=\"trans-container\">.*?<ul>.*?</div>");
Matcher m1 = searchMeanPattern.matcher(result); //m1是獲取包含翻譯的整個<div>的

if (m1.find()) {
String means = m1.group();//所有解釋,包含網頁標簽
Pattern getChinese = Pattern.compile("(?m)<li>(.*?)</li>"); //(?m)代表按行匹配
Matcher m2 = getChinese.matcher(means);

System.out.println("釋義:");
while (m2.find()) {
//在Java中(.*?)是第1組,所以用group(1)
System.out.println("\t" + m2.group(1));
}
} else {
System.out.println("未查找到釋義.");
System.exit(0);
}
}
}二,Python代碼
#!/usr/bin/python
#coding:utf-8
import urllib
import sys
import re

if len(sys.argv) == 1: #沒有單詞就提示用法
print "用法:./Dict.py 要查找的單詞"
sys.exit()

word = ""
for x in range(len(sys.argv) - 1): #查找的可能是短語,中間有空格,如"join in",這裡拼接單詞
word += " " + sys.argv[x + 1]
print "單詞:" + word

searchUrl = "http://dict.youdao.com/search?q=" + word + "&keyfrom=dict.index" #查找的地址
response = urllib.urlopen(searchUrl).read() #獲得查找到的網頁源碼

#從網頁源碼提取出單詞釋義那一部分
searchSuccess = re.search(r"(?s)<div class=\"trans-container\">.*?<ul>.*?</div>",response)

if searchSuccess:
#獲取我們想提取的核心單詞釋義,在只有一個分組的情況下,findall返回這個子組字符串組成的列表
means = re.findall(r"(?m)<li>(.*?)</li>",searchSuccess.group())
print "釋義:"
for mean in means:
print "\t" + mean #輸出釋義
else:

--------------------------------------分割線 --------------------------------------

無需操作系統直接運行 Python 代碼 http://www.linuxidc.com/Linux/2015-05/117357.htm

CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

--------------------------------------分割線 --------------------------------------

Copyright © Linux教程網 All Rights Reserved