Python批量查单词源码
2022/2/5 17:12:53
本文主要是介绍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 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 if __name__=="__main__": uls=read_lst() look_up(uls)
如果不想要双语例句,可以直接把html3那一部分删除就可以了。
二、操作方法视频:
<iframe allowfullscreen="true" data-mediaembed="bilibili" id="OAi0QQpU-1644047726366" src="https://player.bilibili.com/player.html?aid=681355882"></iframe>英语学习必备:Python批量查单词神器
三、源码和附加文件完整下载地址:
http://down.pandengenglish.com/f/364317-541776018-ae7ded
这篇关于Python批量查单词源码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python