Python爬虫--简单的单词查询

时间:2021-10-05 21:24:43

Refer to: https://github.com/gaopu/Python/blob/master/Dict.py

本程序参考自上面Github连接

该程序功能是输入一个单词可以给出这个单词的意思

思路是运用python的urllib库和re正则库

Python2代码如下:

#!/usr/bin/python
#coding:utf-8
import urllib
import sys
import re word = raw_input("请输入单词:") searchUrl = "http://dict.youdao.com/search?q=" + word + "&keyfrom=dict.index" #查找的地址
response = urllib.urlopen(searchUrl).read() #获得查找到的网页源码 searchSuccess = re.search(r"(?s)<div class=\"trans-container\">\s*<ul>.*?</div>",response) #从网页源码提取出单词释义那一部分 if searchSuccess:
means = re.findall(r"(?m)<li>(.*?)</li>",searchSuccess.group()) #获取我们想提取的核心单词释义
print "释义:"
for mean in means:
print "\t" + mean.decode('utf-8').encode('gbk') #输出释义
else:
print "未查找到释义."

运行结果:

Python爬虫--简单的单词查询