Python批量查单词源码

英语学习中最难的恐怕就是记单词了。通常情况下是利用纸质或者电子词典一个一个查,耗费时间长,效率低,本人用python编写了一款批量查单词的软件,利用python中BeautifulSoup强大的爬虫功能,批量获取海词dict.cn上面的单词发音、词性和语义,甚至是例句,代码如下:

一、代码演示:

import requests
from bs4 import BeautifulSoup
import re
def read_lst():
    """读取wordlist.txt中的单词"""
    lst=[]
    with open('wordlist.txt',encoding='utf-8') as f:
        lines=f.readlines()
        lst=[i.strip() for i in lines]
    return lst
def look_up(lst):
    """进行单词查询"""
    n=1
    try:
        for i in lst:
            url=r"http://dict.cn/"+i
            r=requests.get(url,timeout=30).text
            recode=r.encode('utf-8')
            soup=BeautifulSoup(recode,"html.parser")
            #爬取单词发音
            html=soup.find('bdo',lang="EN-US")
            phonetic=html.string
            #爬取汉语语义
            html2=soup.find("ul",class_="dict-basic-ul")
            meaning=html2.text.replace("\n","")
            #爬取例句
            html3=soup.find("div",class_="layout sort")
            sentence=html3.text.strip()
            s=re.sub("\t|\n\n\n","",sentence)
            with open("Wordmeaning.txt","a+",encoding="utf-8") as f:
                f.write(f"{n}. "+i+phonetic+meaning+"\n")
                f.write(s+"\n") #写入例句
            n+=1
    except Exception as exc:
        print(exc)
if __name__=="__main__":
    uls=read_lst()
    look_up(uls)

如果不想要双语例句,可以直接把html3那一部分删除就可以了。

二、操作方法视频:

英语学习必备:Python批量查单词神器

三、源码和附加文件完整下载地址:

http://down.pandengenglish.com/f/364317-541776018-ae7ded

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PythonFun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值