zoukankan      html  css  js  c++  java
  • 随机返回经典语句接口API

    api接口:https://www.liutianyou.com/api/?type=js&charset=utf-8

    可以单独将上面链接,在浏览器中查看效果


    这是get请求,参数:type=js&charset=utf-8  返回一个js方法  方法名为writeText

    如果你想在前端使用,在你想要显示的地方,写上下面两句代码就ok了:

    <script type="text/javascript" src="https://www.liutianyou.com/api/?type=js&charset=utf-8"></script>
    
    <p id="hitokoto" style="text-indent: 25px;"><script>writeText()</script></p>
    View Code

    如果你想在后端使用,可用requests直接访问:

    import requests,re
    con =requests.get('https://www.liutianyou.com/api/?type=js&charset=utf-8')
    print(con.content.decode('utf-8'))
    res = re.findall("document.write('(.*?)');",con.content.decode('utf-8'))
    print(res[0])
    View Code

    顺便给出一个案例:

    设置单位个时间内给好友发消息,如骚扰,如打闹:

    from __future__ import unicode_literals
    from threading import Timer
    from wxpy import *
    import requests
    import random
    import re
    
    bot = Bot()
    
    
    # linux执行登陆请调用下面的这句
    # bot = Bot(console_qr=2,cache_path="botoo.pkl")
    def get_news():
        """获取金山词霸每日一句,英文和翻译"""
        url = "http://open.iciba.com/dsapi/"
        r = requests.get(url)
        content = r.json()['content']
        note = r.json()['note']
        return content, note
    
    def get_words():
        con = requests.get('https://www.liutianyou.com/api/?type=js&charset=utf-8')
        # print(con.content.decode('utf-8'))
        #字节无法匹配,需要解码成字符串
        res = re.findall("document.write('(.*?)');", con.content.decode('utf-8'))
        return res[0]
    
    def send_news():
        try:
            # contents = get_news()
            contents = get_words()
            print('contents is',contents)
            # # 你朋友的微信名称,不是备注,也不是微信帐号。
            my_friend = bot.friends().search('筱凝')[0]
            my_friend.send(contents)
            # # 每86400秒(1天),这里没10秒发送1次
            t = Timer(10, send_news)
            # 为了防止时间太固定,于是决定对其加上随机数
            # ran_int = random.randint(0, 100)
            # t = Timer(86400 + ran_int, send_news)
    
            t.start()
        except:
    
            # 你的微信名称,不是微信帐号。
            my_friend = bot.friends().search('mxh')[0]
            my_friend.send(u"今天消息发送失败了")
    View Code

    参考:

    https://blog.csdn.net/qq_37193537/article/details/89499956

    https://blog.csdn.net/abcnull/article/details/108277795

    https://www.liutianyou.com/260.html

    https://v1.hitokoto.cn/

  • 相关阅读:
    CSS自动控制图片大小的代码
    JS 判断 Radio 单选按钮是否为选中状态 并弹出 值信息
    [转]eclipse github 提交代码
    WEB 开发工具分享
    javaShop的一些总结
    前端进阶试题(css部分)
    如何判断浏览器 然后针对不同的浏览器加入单独的样式
    jquery 提示信息显示后自动消失的具体实现
    html a标签 图片边框和点击后虚线框的有关问题
    html 包含一个公共文件
  • 原文地址:https://www.cnblogs.com/mxhmxh/p/10764085.html
Copyright © 2011-2022 走看看