python爬蟲實現成語接龍1.0
效果圖
廢話不多說直接上程式碼…
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.靠簡單的邏輯實現,程式碼需要優化
歡迎大佬提建議!!虛心學習
相關文章
- 趣味成語接龍游戲裡,如何判斷使用者輸入的成語接龍成功?
- Python爬蟲教程-05-python爬蟲實現百度翻譯Python爬蟲
- python的爬蟲功能如何實現Python爬蟲
- Python爬蟲是如何實現的?Python爬蟲
- python實現selenium網路爬蟲Python爬蟲
- 爬蟲——爬取貴陽房價(Python實現)爬蟲Python
- Python爬蟲的兩套解析方法和四種爬蟲實現Python爬蟲
- Python 爬蟲IP代理池的實現Python爬蟲
- Python實現微博爬蟲,爬取新浪微博Python爬蟲
- Python 爬蟲實戰Python爬蟲
- Python爬蟲之XPath語法Python爬蟲
- 【Python學習】爬蟲爬蟲爬蟲爬蟲~Python爬蟲
- Python爬蟲教程-06-爬蟲實現百度翻譯(requests)Python爬蟲
- Python成為爬蟲常用語言的原因是什麼?Python爬蟲
- python爬蟲是什麼?為什麼用python語言寫爬蟲?Python爬蟲
- python爬蟲實戰,爬蟲之路,永無止境Python爬蟲
- python爬蟲簡單實現逆向JS解密Python爬蟲JS解密
- python爬蟲-33個Python爬蟲專案實戰(推薦)Python爬蟲
- Python《爬蟲初實踐》Python爬蟲
- python爬蟲實戰教程-Python爬蟲開發實戰教程(微課版)Python爬蟲
- 不踩坑的Python爬蟲:Python爬蟲開發與專案實戰,從爬蟲入門 PythonPython爬蟲
- Python之分散式爬蟲的實現步驟Python分散式爬蟲
- python爬蟲---網頁爬蟲,圖片爬蟲,文章爬蟲,Python爬蟲爬取新聞網站新聞Python爬蟲網頁網站
- python3網路爬蟲開發實戰_Python3 爬蟲實戰Python爬蟲
- python就是爬蟲嗎-python就是爬蟲嗎Python爬蟲
- python網路爬蟲應用_python網路爬蟲應用實戰Python爬蟲
- python爬蟲:XPath語法和使用示例Python爬蟲
- Python網路爬蟲實戰Python爬蟲
- python 爬蟲實戰的原理Python爬蟲
- Python爬蟲實戰之bilibiliPython爬蟲
- python 爬蟲 實現增量去重和定時爬取例項Python爬蟲
- python如何實現簡單的爬蟲功能?Python學習教程!Python爬蟲
- python 爬蟲Python爬蟲
- python爬蟲Python爬蟲
- python 爬蟲 簡單實現百度翻譯Python爬蟲
- 使用Python爬蟲實現自動下載圖片Python爬蟲
- Requests如何在Python爬蟲中實現get請求?Python爬蟲
- 汪子熙趣味成語接龍游戲的設計初衷