because I want to test English four, so I get up early in the morning by the English words, but as the dregs of English, I can only be on the Web page to look up the meaning of the word. Check more, heart tired, then think how to look at the terminal under the word, so the speed is not fast?
Now, I look closely at each query, the URL in the browser address bar changes, found each time the browser submitted the URL is "http://www.youdao.com/w/eng/" xxxxx "/#keyfrom =dict2.index" ( One of the XXXXX represents the word to look for), with this discovery, then we download the URL point to the page, and then extract the information we need to do it?
Emmmm, this job is the most appropriate thing to do with Python.
Basic steps: Import the requests module and the RE module, first download the URL address to the webpage, and then use Python's powerful regular expression to extract the translated information of the word. Because I want to translate between English and Chinese, I have written two pieces of code, the first one is Chinese to English, the second is English translation.
Source
Translation
ImportRequests,re
defDownload (): Word=input ("Please enter the Chinese words you want to translate: \ n") URL="http://dict.youdao.com/w/eng/"+word+"/#keyfrom =dict2.index" #合并URL地址HTML=requests.get (URL). Content.decode ('Utf-8') #得到服务器的相应信息后将其转码为UTF-8 returnHTMLdefAnalysis (): List1=re.findall ("detailed explanation. +<p class=\ "collapse-content\" >", Download (), Re. S) #这里对html字符串进行第一步加工, intercept the approximate information list2=re.findall ("[A-za-z]+", str (LIST1)) #将上面加工后的字符串进一步加工, extract all translated word information directly Print("translation result: \ n") forIinchlist2:i=I.strip () #因为第二步加工后的信息并不干净, the resulting word will be preceded by a space, where the space is deletedPrint(i)if __name__=='__main__': while (1):
Analysis ()
English-Chinese Translation:
ImportRequests,redefDownload (): Word=input ("Please enter the English word you want to translate: \ n") URL="http://dict.youdao.com/w/eng/"+word+"/#keyfrom =dict2.index"HTML=requests.get (URL). Content.decode ('Utf-8') returnHTMLdefAnalysis (): List1=re.findall ("detailed explanation. +<p class=\ "collapse-content\" >", Download (), Re. S) List2=re.findall ("\w+", str (LIST1)) #只有此处代码与汉译英代码不同, because it is the extraction of Chinese characters, so here to use \w to match Chinese characters Print("translation result: \ n") forIinchlist2:i=I.strip ()Print(i)if __name__=='__main__':
while (1):
Analysis ()
OK, let's take a look at the effect:
Emmmm,ok, after sending the blog to continue to recite the word "sad " "sad" "sad"
Python implements a dictionary of search Youdao