Python将文本转换为mp3

2024-05-01 04:58:12 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在从网站上抓取数据并将文本转换为mp3,但每次都会出现以下错误:

Traceback (most recent call last):
  File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/awais/.local/lib/python2.7/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/awais/Desktop/sipgateio-incomingcall-python-master/incoming_call/__main__.py", line 93, in handle_on_dtmf
    ttmp3.save("bbc.mp3")
  File "/home/awais/.local/lib/python2.7/site-packages/gtts/tts.py", line 295, in save
    self.write_to_fp(f)
  File "/home/awais/.local/lib/python2.7/site-packages/gtts/tts.py", line 272, in write_to_fp
    raise gTTSError(tts=self)
gTTSError: Failed to connect. Probable cause: Host 'https://translate.google.en/' is not reachable

这就是我的代码的样子:

URL = requests.get("https://www.bbc.co.uk/news")

    soup = BeautifulSoup(URL.text, 'html.parser')


    headlines = soup.select(".gs-c-promo-heading__title")
    all = ""
    for h in headlines:
        h=h.text
        h = h.encode('ascii', 'ignore').decode('ascii')
        all = str(all) + "  " + str(h)
    print(all)
    ttmp3 = gTTS(all, "en")
    ttmp3.save("bbc.mp3")

我被困在这里一天了,请帮帮我


Tags: inpyselfappflaskhomerequestlib