python实现一个字典
生活随笔
收集整理的這篇文章主要介紹了
python实现一个字典
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/usr/bin/python
#-*-coding:utf-8-*-from selenium import webdriver
from bs4 import BeautifulSoup
#上面兩個都是第三方庫,請自行下載,還有PhantomJS也要去官網下載安裝
def main():while True:word = raw_input("輸入要查詢的單詞:")driver = webdriver.PhantomJS(r'C:\phantomjs-2.1.1-windows\bin\phantomjs.exe')url = "https://www.baidu.com/s?wd=%s"%worddriver.get(url)#下載網頁并且渲染Javascriptsoup = BeautifulSoup(driver.page_source,"html.parser")#用Beautiful解析網頁detail = soup.select("table.op_dict3_english_result_table tbody tr td span")sentence = soup.select("div.op_dict3_lineone_result.c-clearfix span")chinese = soup.find('div',attrs={"class":"op_dict3_linetwo_result"})if len(detail)==0 or sentence==0:print "解析錯誤,請確定輸入的是英語單詞"continuewordDict = {}for i in range(0,len(detail),2):try:wordDict[detail[i].get_text().strip()]=detail[i+1].get_text().strip().replace("\n","")except:breakfor key in wordDict.keys():if '[' in key:print wordDict[key].replace(" "*6,"")continueprint key+" "+wordDict[key]for i in sentence:print i.get_text()if chinese!=None:print chinese.get_text()
if __name__== "__main__":main()
總結
以上是生活随笔為你收集整理的python实现一个字典的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java:单词倒排
- 下一篇: 破解简单的Android签名检查