随机返回经典语句接口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/

posted @ 2019-04-24 18:26  m*x*h  阅读(2379)  评论(0编辑  收藏  举报