Forvo免费api

Forvo是一个收集母语发音的网站,从这个网站里面你不仅可以得到标准读音,还能得到各种不标准读音。因为官方api是付费的,所以研究了一下网页。

#!/usr/bin/env python3
import re
import requests
import json
import base64

# this function return a json, the url prefix is *https://forvo.com/mp3/*
def getEnglishPronunciation(word):
    webPageUrl = "https://forvo.com/word/%s/#en" % word
    webPageText = requests.get(webPageUrl).text
    englishPageTextList = re.findall("<em id=\"en.*?</article>", webPageText, re.DOTALL)
    if len(englishPageTextList) == 0:
        return '{"status":"error"}'
    englishPageText = englishPageTextList[0]
    pronunciations = re.findall("Play\(\d+,'(.*?)'", englishPageText)
    for l in range(len(pronunciations)):
        pronunciations[l] = base64.b64decode(pronunciations[l]).decode()
    return json.dumps(pronunciations)

# print(getEnglishPronunciation("naive"))