3rw2 发表于 2014-8-1 08:36:26

【python】简单的网页内容获取 - 有道翻译英文


正则表达式与python的网页操作练习一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)
else:
print('not found!')






显示如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> (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. 要求,请求



>>>



页: [1]
查看完整版本: 【python】简单的网页内容获取 - 有道翻译英文