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,text,单词,soup,lst,源码 来源: https://blog.csdn.net/henanlion/article/details/122790782