Java and Python use Youdao dictionaries to make search word scripts
Two pictures first, look at the effect.
In Java:
Python's:
Today's whim, want to do a check word of things, hurriedly go Youdao dictionary website looked, the original we want to query the word is embedded in the Web address to the Youdao dictionary, and then the result of the page is the word we need to explain, so this thing need technical knowledge only:
Regular expressions
What we want to do is to extract the word from the source of the Web page, so we only say the regular expression of the word interpretation.
Analysis of the source code, we can see that the word interpretation are in a div tag, as shown:
The primary goal is to get this part, and the regular expression can be written like this:
(s) the meaning of <div class=\ "Trans-container\" >.*?<ul>.*?</div>
//(s) is to make '. ' Can match line breaks, default is mismatched
//.*? means to match any number of characters in a non-greedy mode
After getting to this part, further, what we need is the meaning of the words inside, so we can do this:
(? m) <li> (. *?) </li>
//(? m) meaning is matched by rows, in which no row is matched by this regular expression, the default is not branch, the uniform match
//here with parentheses. *? wrap it up so that you can directly get the meaning of the word and give away the label next to it
The following is the specific code: One, Java code
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 ("Please enter the word you want to check:");
Scanner s = new Scanner (system.in);
String Word = S.nextline ();
Word = Word.replaceall ("", "+"); Find an address based on a lookup word construct httpget Getwordmean = new HttpGet ("http://dict.youdao.com/search?q=" + Word + "&keyfrom=dict.in")
Dex "); Closeablehttpresponse response = Httpclient.execute (Getwordmean);//Get the returned page source String result = entityutils.tostring
(Response.getentity ()); response.clOSE (); Pay attention to (s), meaning let '. ' Match line breaks, default mismatch pattern Searchmeanpattern = Pattern.compile ("(? s) <div class=\" Trans-container\ ">.*?<ul>
.*?</div> "); Matcher m1 = Searchmeanpattern.matcher (result); M1 is to obtain an if (M1.find ()) {String means = M1.group () of the entire <div> that contains translations, including page labels Patte RN Getchinese = Pattern.compile ("(? m) <li> (. *?) </li> ");
(? m) represents the matching of Matcher m2 = getchinese.matcher (means) by line;
System.out.println ("Interpretation:"); while (M2.find ()) {//in Java (. *?)
Is the 1th group, so use Group (1) System.out.println ("T" + m2.group (1));
} else {System.out.println ("Not Found interpretation.");
System.exit (0); }
}
}
Two, Python code
#!/usr/bin/python
#coding: utf-8
import urllib
import sys
import re
If Len (sys.argv) = 1: # No words prompt usage
print "usage:./dict.py words to find"
sys.exit ()
word = "" for
x in range (len (SYS.ARGV)-1): #查找的可能是短语 , there are spaces in the middle, such as "Join in", where the word +
+ "+ sys.argv[x + 1] print" word + + "" +
word
searchurl = "http://dict.youdao.c Om/search?q= "+ word +" &keyfrom=dict.index " #查找的地址
response = Urllib.urlopen (Searchurl). Read () # Get the search page source
#从网页源码提取出单词释义那一部分
searchsuccess = Re.search (r) <div class=\ "Trans-container\" >.*? <ul>.*?</div> ", response)
if searchsuccess:
#获取我们想提取的核心单词释义, in the case of only one grouping, FindAll Returns a list of this subgroup string
means = Re.findall (? m) <li> (. *?) </li> ", Searchsuccess.group ())
print" Explanation: "for
mean in means:
print" \ t "+ mean #输出释义
else:
print "no explanation found."