【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. 要求,请求



>>>
相关文章
相关标签/搜索