看英文pdf的時候,遇到不認(rèn)識的單詞,都是復(fù)制到網(wǎng)頁翻譯,很頭疼。因為系統(tǒng)是win10,高分屏,有道的取詞效果垃圾,一直想做一個python詞典,在github看到這個,https://github.com/llf2017/pyDict/blob/master/pyDict.py,跟我想做的差不多。
因為不喜歡用BS,想把提取有道文本的那兩行重新寫下。
首先
import lxml
然后:
url ='http://dict.youdao.com/w/{}/'.format(word)
res = get_url(url)
tree = html.fromstring(res.content)
分析有道返回的數(shù)據(jù)(查詢share這個單詞):

寫出下面的xpath表達(dá)式:
data = tree.xpath('//div[@id="results"]//div[@class="trans-container"]//ul/li[not(contains(text(),"\n"))]/text()')
# contains函數(shù),contains(string1,string2),如果string1包含string2,則返回true
# //li[not(contains(text(),"\n"))] 返回文本不包括 \n 的 li
直接可以得到單詞中文結(jié)果的列表