python爬蟲實現成語接龍1.0

Elric_01發表於2020-10-06

效果圖
在這裡插入圖片描述

廢話不多說直接上程式碼…

import random
import requests
from bs4 import BeautifulSoup
from pypinyin import lazy_pinyin
from Pinyin2Hanzi import DefaultDagParams
from Pinyin2Hanzi import dag


class Spider(object):
    base_url = 'http://www.chengyujielong.com.cn/search/'

    def get_url(self, word):
        url = self.base_url + word
        return url

    def get_idiom(self, url):
        response = requests.get(url)
        html = response.content.decode()
        soup = BeautifulSoup(html, 'lxml')
        result = int((soup.find('h1').get_text())[3])

        if result != 0:
            content = soup.select('#main > div:nth-child(9) > div.panel-body > ul > li')

            idiom_list = []
            for li in content:
                idiom = li.string
                idiom_list.append(idiom)
            return idiom_list
        else:
            return False

    def main(self, word):
        url = spider.get_url(word)
        result = spider.get_idiom(url)
        return result


class HanziPinyin(object):
    def hanzi_2_pinyin(self, old_hanzi):
        old_pinyin = lazy_pinyin(old_hanzi)
        return old_pinyin

    def pinyin_2_hanzi(self, old_pinyin):
        dagParams = DefaultDagParams()
        # 個候選值
        hanzi_list = dag(dagParams, old_pinyin, path_num=5, log=True)
        new_word = []
        for hanzi in hanzi_list:
            new_word.append(hanzi.path[0])
        word = random.sample(new_word, 1)[0]
        return word


def player(name, idiom):
    old_word = idiom[-1:]

    result1 = spider.main(old_word)

    if not result1:
        # 如果沒有同音字就進行諧音字轉換
        # 諧音字轉換
        pinyin = change_word.hanzi_2_pinyin(old_word)
        search_word = change_word.pinyin_2_hanzi(pinyin)
        result2 = spider.main(search_word)
        if not result2:
            return 0
        else:
            send_idiom = random.sample(result2, 1)[0]
            print('%s:%s' % (name, send_idiom))
            return send_idiom,

    else:
        send_idiom = random.sample(result1, 1)[0]
        print('%s:%s' % (name, send_idiom))
        return send_idiom


if __name__ == '__main__':
    spider = Spider()
    change_word = HanziPinyin()
    count = 0
    print('\n成語接龍開始!\n')
    result = input('請輸入開始接龍的成語:')

    while result != 0:

        result = player('Elric', result)
        if result == 0:
            break
        result = player('Edward', result)
        count += 1

    print('遊戲結束,共進行了%d輪' % count)

有待改進:
1.網站會返回單個字或者其他詞語,而不是成語
2.有時候可以接但是卻停下了,因為隨機取字的原因導致諧音也取了生僻字
3.靠簡單的邏輯實現,程式碼需要優化

歡迎大佬提建議!!虛心學習

相關文章