隨機返回經典語句接口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://www.liutianyou.com/260.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM