【python】簡單的網頁內容獲取 - 有道翻譯英文


正則表達式與python的網頁操做練習一:html

import urllib.request
import re

qname=input('input english:')
qname=qname.strip()
url='http://dict.youdao.com/search?le=eng&q='+qname+'&keyfrom=dict.top'
html=urllib.request.urlopen(url)
source=html.read().decode('UTF-8')

reg='(?<=<div class="trans-container">)(.*?)(?=</div>)'

r=re.compile(reg,re.S)
m=r.search(source)


repl='</?[^>]*>|^$'
r1=re.compile(repl)

if m:
cn=r1.subn('',m.group(1))
print(cn[0])
else:
print('not found!')



 顯示以下:python

>>> (executing lines 1 to 23 of "fy.py")
input english:python



n. 巨蟒;大蟒
n. (法)皮東(人名)



>>> (executing lines 1 to 23 of "fy.py")
input english:request



n. 請求;須要
vt. 要求,請求



>>>
相關文章
相關標籤/搜索