'''
一、文字转语音api,树莓派天气闹钟
爬取实时天气数据转换为语音,设置树莓派计划任务
'''
from aip import AipSpeech
import requests
import re
from bs4 import BeautifulSoup
import time
from datetime import datetime
import os
''' '''
def getHtmlText(url,code='utf-8'):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = code
return r.text
except:
return ''
def makeSoup(html):
wstr = ''
if html == '':
return '嘤嘤嘤~今天我也不知道海淀天气了'
else:
soup = BeautifulSoup(html,'html.parser')
soup1 = soup.find_all('li',attrs = {'class':'on'})[1]
str1 = re.findall(r'>(.*)</',str(soup1))
b = ''
try:
slist = re.findall(r'^(.*)</span>(.*)<i>(.*)$',str1[4])
for x in range(len(slist[0])):
b += slist[0][x]
except:
b = str1[4]
if '/' in b:
b = b.replace('/','-')
str1[4] = '海淀的温度是'+b
str1[6] = ',风力是'+str1[6]
donser= str1[6]
if "&lt" in donser:
donser=donser.replace("&lt"," ")
str1[6]=donser
for i in str1:
print(i[:])
if i != '':
if i[0]=='&':
# print("###")
print("")
wstr = wstr +i
if '雨' in wstr:
wstr += ',出门别忘记带雨伞,嘤嘤嘤!'
#print(wstr)
return wstr
'''
用百度的AIP
把文字变成mp3文件
'''
def stringToMp3(strings_txt):
week=str(datetime.now().weekday()+1)
if week=="":
week="日"
month=str(datetime.now().strftime('%m'))
if month[0]=='':
month=month[1:]
strings_txt = 'Surprise 兄dei,起床啦~嘤嘤嘤~起床啊~啊~啊~啊~嘤嘤嘤~起床啦~要迟到啦!今天是星期'+ week + "," + str(month) +"月~"+ strings_txt
print(strings_txt)
APPID = ''
APIKey = 'iKX6hukjx9vRPo4VT6x3POKw'
SecretKey = 'pI0VoAXi11YXDtmxyXe2rffUQWvgNgGy' aipSpeech = AipSpeech(APPID,APIKey,SecretKey)
result = aipSpeech.synthesis(strings_txt,'zh','',\
{'vol':8,
'per':4,
'spd':5})
if not isinstance(result,dict):
with open('tep.mp3','wb') as f:
f.write(result) '''
MAIN
'''
def main():
url = 'http://www.weather.com.cn/weather/101010200.shtml'
html=getHtmlText(url)
stringToMp3(makeSoup(html))
os.system('mpg321 tep.mp3')
os.system('mpg321 tep.mp3')
#os.system('rm -rf tep.mp3') if __name__ == '__main__':
main()

crontab -e 设置计划任务        8 22 * * * python3 /home/pi/weather.py          #分 时 日 月 周

点击  https://files.cnblogs.com/files/dzzy/tep.zip 查看效果

 
'''
二、百度api图片转文字
'''

# -*- coding: UTF-8 -*-

from aip import AipOcr

APP_ID = ''
API_KEY = 'QGGvDG2yYiVFvujo6rlX4SvD'
SECRET_KEY = 'PcEAUvFO0z0TyiCdhwrbG97iVBdyb3Pk' aipOcr=AipOcr(APP_ID, API_KEY, SECRET_KEY) filePath = "shit.jpg" def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read() options = {
'detect_direction': 'true',
'language_type': 'CHN_ENG',
} result = aipOcr.webImage(get_file_content(filePath),options) # url调用
# result = apiOcr.webImage('http://www.?????.com/????.jpg') print(result)

百度 api 测试 &amp; python-LMLPHP

结果: [{'words': '秘密'}, {'words': 'su seven'}, {'words': '我有很多秘密我一个人细数'}, {'words': '从欢笑到哭泣'}], 'words_result_num': 4}

05-20 18:22