62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class jk {
public static String sendGet(String url, String param) throws IOException {
String result = "";
BufferedReader in = null;
String urlName = url + param;
URL realUrl = new URL(urlName);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0(compatible;MSIE 6.0;Window NT 5.1;SV1)");
conn.connect();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(),
"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
return result;
}
public static void main(String args[]) throws IOException {
String s = jk.sendGet("http://dict-co.iciba.com/api/dictionary.php?w=",
"word");
System.out.println(s);
System.out.print(wordMeanning(s));
}
public static String wordMeanning(String word) {
Pattern p = Pattern.compile("<acceptation>(.*?)</acceptation>");
Matcher m = p.matcher(word);
StringBuffer wordMeanning = new StringBuffer();
while (m.find()) {
wordMeanning.append(m.group(1));
wordMeanning.append(", ");
}
return wordMeanning.delete(wordMeanning.length() - 2,
wordMeanning.length() - 1).toString();
}
}
String s1 = "<acceptation>";
String s2 = "</acceptation>";
String s3 = s.substring(s.indexOf(s1) + s1.length(), s.indexOf(s2));
public class jk {
public static String sendGet(String url, String param) throws IOException {
String result = "";
BufferedReader in = null;
String urlName = url + param;
URL realUrl = new URL(urlName);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0(compatible;MSIE 6.0;Window NT 5.1;SV1)");
conn.connect();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
FileWriter fw = new FileWriter("src/a.txt");
while ((line = in.readLine()) != null) {
result += "\n" + line;
fw.write(line);
}
fw.close();
return result;
}
public static void main(String args[]) throws IOException {
String s = jk.sendGet("http://dict-co.iciba.com/api/dictionary.php?w=",
"word");
byte[] b = s.getBytes("ISO-8859-1");
s = new String(b, "UTF-8");
System.out.print(s);
}
}
<?xml version="1.0" encoding="UTF-8"?><dict num="219" id="219" name="219"><key>word</key><ps>w?:d</ps><pron>http://res.iciba.com/resource/amp3/c/4/c47d187067c6cf953245f128b5fde62a.mp3</pron><pos>n.</pos><acceptation>?, ?, ?, ??, ??, ??</acceptation><pos>vt.</pos><acceptation>?...??</acceptation><sent><orig>smear word</orig><pron>http://res.iciba.com/resource/phrase_mp3/9/2/92aa6d061c7a42c045dfe255e54b68a5.mp3</pron><trans>??????</trans></sent><sent><orig>new word</orig><pron>http://res.iciba.com/resource/phrase_mp3/d/0/d018ff527432e94d60b959410aa4523c.mp3</pron><trans>??</trans></sent><sent><orig>word picture</orig><pron>http://res.iciba.com/resource/phrase_mp3/4/9/499ee15da204af581a41c26728702c3a.mp3</pron><trans>???????</trans></sent><sent><orig>unfamiliar word</orig><pron>http://res.iciba.com/resource/phrase_mp3/5/5/55f564a2ef3f2a27e44c3cfffe7fef24.mp3</pron><trans>??</trans></sent><sent><orig>word stress</orig><pron>http://res.iciba.com/resource/phrase_mp3/5/d/5d61b1e8dc6c4bc322e49d78ddc63479.mp3</pron><trans>????</trans></sent></dict>
<?xml version="1.0" encoding="UTF-8"?><dict num="219" id="219" name="219"><key>word</key><ps>wə:d</ps><pron>http://res.iciba.com/resource/amp3/c/4/c47d187067c6cf953245f128b5fde62a.mp3</pron><pos>n.</pos><acceptation>字, 词, 话, 消息, 诺言, 命令</acceptation><pos>vt.</pos><acceptation>为...措辞</acceptation><sent><orig>smear word</orig><pron>http://res.iciba.com/resource/phrase_mp3/9/2/92aa6d061c7a42c045dfe255e54b68a5.mp3</pron><trans>诬蔑性的字眼</trans></sent><sent><orig>new word</orig><pron>http://res.iciba.com/resource/phrase_mp3/d/0/d018ff527432e94d60b959410aa4523c.mp3</pron><trans>生词</trans></sent><sent><orig>word picture</orig><pron>http://res.iciba.com/resource/phrase_mp3/4/9/499ee15da204af581a41c26728702c3a.mp3</pron><trans>生动的文字描述</trans></sent><sent><orig>unfamiliar word</orig><pron>http://res.iciba.com/resource/phrase_mp3/5/5/55f564a2ef3f2a27e44c3cfffe7fef24.mp3</pron><trans>冷字</trans></sent><sent><orig>word stress</orig><pron>http://res.iciba.com/resource/phrase_mp3/5/d/5d61b1e8dc6c4bc322e49d78ddc63479.mp3</pron><trans>单词重音</trans></sent></dict>