百度 api 测试

Posted Donser

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了百度 api 测试相关的知识,希望对你有一定的参考价值。


\'\'\'
一、文字转语音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}

 

以上是关于百度 api 测试的主要内容,如果未能解决你的问题,请参考以下文章

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段

百度天气API使用测试

百度 api 测试

百度文心大模型API测试

使用百度定位Api获取当前用户登录地址

onActivityResult 未在 Android API 23 的片段上调用