百度 api 测试 & python

 
   
'''
一、文字转语音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=="7":
        week=""
    month=str(datetime.now().strftime('%m'))
    if month[0]=='0':
        month=month[1:]
    strings_txt = 'Surprise 兄dei,起床啦~嘤嘤嘤~起床啊~啊~啊~啊~嘤嘤嘤~起床啦~要迟到啦!今天是星期'+ week + "," + str(month) +"月~"+ strings_txt
    print(strings_txt)
    APPID = '11688959'
    APIKey = 'iKX6hukjx9vRPo4VT6x3POKw'
    SecretKey = 'pI0VoAXi11YXDtmxyXe2rffUQWvgNgGy'
 
    aipSpeech = AipSpeech(APPID,APIKey,SecretKey)
    result = aipSpeech.synthesis(strings_txt,'zh','1',\
                                {'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 = '10379743' 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)

 

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

 

转载于:https://www.cnblogs.com/dzzy/p/9846637.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值