生成器游戏

藏头诗

思路:利用百度汉语这个接口爬取诗句,然后存到列表中随机抽出对应的一句并打印。

主要参数

python-藏头诗与成语接龙(爬虫)-LMLPHP

完整代码

import requests
from random import choice

class Hide:
    '''藏头诗类'''
    def __init__(self):
        print("=" * 20 + "藏头诗生成器" + "=" * 20)
        self.url = 'https://hanyu.baidu.com/hanyu/ajax/search_list?'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36 Edg/81.0.416.68'
        }
    def get_ct(self):
            # 输入需要藏头的语句
            print("输入要生成的藏头句子:")
            self.wd = input('')
            for head in self.wd:
                try:
                    params = {'wd':head+'开头的诗句',
                          'from':'poem',
                          'pn':1,}
                    res = requests.get(url=self.url,headers=self.headers,params=params)
                    # 七言
                    list7 = []
                    # 五言
                    list5 = []
                    ret_array = res.json()['ret_array']
                    for ret in ret_array:
                        poem = ret['display_name'][0]
                        # 判断诗句长度
                        if len(poem) == 7:
                            list7.append(poem)
                        elif len(poem) == 5:
                            list5.append(poem)
                    print(choice(list7))
                except:
                    print("没有找到符合要求的诗句")
class Idiom:
    '''成语接头类'''
    def __init__(self):
        print("="*20+"成语接头生成器"+"="*20)
        self.url = self.url = 'https://hanyu.baidu.com/hanyu/ajax/search_list?'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36 Edg/81.0.416.68'
        }
    # 存放成语
    list4 = []
    def get_cy(self):
        try:
            self.wd = input("")
            while True:
                params2 = {
                    "wd": self.wd[-1] + "开头的成语",
                    "device": "pc",
                    "from": "home",
                    "pn": 1,
                }
                res2 = requests.get(url=self.url,headers=self.headers,params=params2)
                ret_array = res2.json()['ret_array']
                for ret2 in ret_array:
                    idiom2 = ret2["name"][0]
                    if len(idiom2) == 4:
                        self.list4.append(idiom2)
                cy_tail = choice(self.list4)
                print(cy_tail)
                # 删除所有成语
                del self.list4[:]
                self.wd = input("")
                if self.wd == "0":
                    print("下次再见")
                    break
                if self.wd[-1] != cy_tail[-1]:
                    print("没对上尾,这次就交给我吧")
                    continue

        except KeyError:
            print("没有找到匹配的成语")

# 主程序
while True:
    play = int(input("想玩什么?\n(1:藏头诗,2:成语接龙,0:退出):"))
    if play == 1:
        Hide().get_ct()
        while True:
            a = input('\n输入a继续(回车退出):')
            if a == 'a':
                for i in range(4):
                    Hide().get_ct()
            else:
                break
    elif play == 2:
        Idiom().get_cy()
    else:
        print("退出游戏")
        break

运行结果

python-藏头诗与成语接龙(爬虫)-LMLPHP

成语接龙(2020.9.9更新)

python-藏头诗与成语接龙(爬虫)-LMLPHP

09-11 14:03