import requests
from io import BytesIO
from urllib.parse import quote
from docx import Document
import time
# 修改cookie
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate, br',
'cookie': '...',
'Upgrade-Insecure-Requests': '1',
'TE': 'Trailers',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'connection': 'keep-alive',
}
URL = "https://fanyi.baidu.com/gettts?lan=en&text=%s&spd=3&source=web"
session = requests.session()
maxlen = 2
def fanyi(no, sentence):
"""
:param no: 序號(hào)
:param sentence: 一段英文
:return:
"""
print(sentence, end=" ")
response = session.get(URL % quote(sentence), headers=headers)
print("code: ", response.status_code)
if response.status_code != 200:
return False
with open('./2021-03-08/' + str(no) + ".mp3", "wb") as f:
soud = BytesIO(response.content)
z = soud.getbuffer()
f.write(z)
f.flush()
return True
def main():
# 這個(gè)函數(shù)主要是為了獲取文件中的英文,自行修改
document = Document('./2021-03-08.docx')
cnt = 0
for para in document.paragraphs:
text = para.text
start, end = text.find('】'), text.find('^')
if start == -1 or end == -1:
continue
cnt += 1
no = str(cnt)
if len(no) < maxlen:
no = '0' * (maxlen - len(no)) + no
fanyi(no, text[start + 1:end].strip())
time.sleep(3)
print(cnt)
if __name__ == '__main__':
main()
|