?如何为英语单词批量添加音标?一个个的上网查?早OUT了吧!
利用PYTHON中的beautiful soup模块那就小菜一碟了。
# -*-coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
f = open('words.txt', encoding='utf-8') # 打开当前文件位置下的文本文档,每行一个英文单词
fw = open('./result.txt', 'a', encoding='utf-8')
index = 0
for word in f.readlines():
index = index+1
url = "https://www.oxfordlearnersdictionaries.com/definition/english/" + word.strip() # 待查单词的牛津词典网页
print("当前正在查第{}个单词:".format(index), word)
wbdata = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36'}).text # 获取相应网页代码
soup = BeautifulSoup(wbdata, 'html.parser') # BS解析代码
news_titles = soup.select('span[class="phon"]') # 选取其中的发音代码段,结果为英美两种发音,所以类型为列表
print(word.strip(), news_titles[0].text, sep='\t', file=fw) # 每行的英文单词后带有换行标志,所以strip清除掉,选取一个发音的文本,并写到result文本中
fw.close()
f.close()
|