Python and python
I got up early in the morning and got into English words because I needed to take the CET4 test. However, I can only find words on the webpage. I'm tired of searching too much, so I thought about how to query words on the terminal. It's not so fast?
NOW, I carefully observe the URL changes in the browser address bar during each query, found that each browser submitted URL is "http://www.youdao.com/w/eng/" xxxxx "/# keyfrom = dict2.index" (where xxxxx represents the word to be queried), with this discovery, so we can download the webpage that the URL points to and extract the information we need?
Emmmm. It is most appropriate to give this job to Python.
Basic Steps: import the requests module and re module. First, download the webpage directed to the URL address, and then extract the translated information of words using the powerful Python regular expression. Because of the need for English-Chinese translation, I wrote two pieces of code: the first is Chinese-English Translation and the second is English-Chinese translation.
Source code
English to Chinese:
Import requests, re
Def download (): word = input ("Enter the Chinese word you want to translate: \ n ") url = "http://dict.youdao.com/w/eng/" + word + "/# keyfrom = dict2.index" # merge URL url html = requests. get (url ). content. decode ('utf-8') # Get the server information and then transcode It To The UTF-8 return htmldef analysis (): list1 = re. findall ("detailed description. + <p class = \ "collapse-content \"> ", download (), re. s) # Here we perform the first step of processing the html string, intercepting the approximate information list2 = re. findall ("[a-zA-Z] +", str (list1) # further process the processed string, print ("translation result: \ n") for I in list2: I = I. strip () # because the information after processing in step 2 is not clean, there will be a space before the obtained word. Here, print (I) is deleted from the space) if _ name _ = '_ main _': while (1 ):
Analysis ()
English to Chinese:
Import requests, redef download (): word = input ("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') return htmldef analysis (): list1 = re. findall ("detailed description. + <p class = \ "collapse-content \"> ", download (), re. s) list2 = re. findall ("\ w +", str (list1) # Only the code here is different from the Chinese-English code, because it is used to extract Chinese characters, therefore, we need to use \ w to match the Chinese character print ("translation result: \ n") for I in list2: I = I. strip () print (I) if _ name _ = '_ main __':
While (1 ):
Analysis ()
OK. Let's see the effect:
Emmmm, OK. After the blog is sent, you must continue to recite the words "sad" and "sad" and "sad]