Python2.爬虫--单词查询程序

参考传送门:
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 "未查找到释义."

运行结果:
 结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值