超詳細(xì),手把手教你用20行Python代碼制作飛花令小程序!
飛花令是古時(shí)候人們經(jīng)常玩一種“行酒令”的游戲,是中國(guó)古代酒令之一,屬雅令?!帮w花”一詞則出自唐代詩(shī)人韓翃《寒食》中 春城無(wú)處不飛花 一句。行飛花令時(shí)選用詩(shī)和詞,也可用曲,但選擇的句子一般不超過(guò)7個(gè)字。
在《中國(guó)詩(shī)詞大會(huì)》中改良了“飛花令”,不再僅用 花 字,而是增加了 云、 春、月、夜 等詩(shī)詞中的高頻字,輪流背誦含有關(guān)鍵字的詩(shī)句,直至決出勝負(fù)。
今天,我們就利用 Python 定制一款“飛花令”小程序:給定一個(gè)關(guān)鍵字或者關(guān)鍵詞,就能夠返回許多含有這個(gè)關(guān)鍵字的詩(shī)句,跟朋友玩再也不怕輸了!
網(wǎng)頁(yè)分析
要利用爬蟲完成這項(xiàng)工作需要先選擇一個(gè)合適的網(wǎng)站,這里我們選擇了 古詩(shī)文網(wǎng)https://www.gushiwen.cn/
在右上角的方框中輸入關(guān)鍵詞,如酒,就能夠返回相應(yīng)的結(jié)果:
我們注意到,返回的結(jié)果是一整首詩(shī)或詞,關(guān)鍵字所在的句子僅為其中一句。后面我們爬取信息時(shí)也需要做到過(guò)濾。
往下翻頁(yè)后會(huì)發(fā)現(xiàn)只能獲取前 2 頁(yè)內(nèi)容,到第 3 頁(yè)會(huì)出現(xiàn)以下提示:
也就是說(shuō)要完整獲取全部詩(shī)文需要下載 App,本文簡(jiǎn)化問(wèn)題只爬取前 2 頁(yè)的內(nèi)容,后續(xù)有機(jī)會(huì)再分享 App 相關(guān)爬蟲推文。在翻頁(yè)的過(guò)程中我們注意一下 URL 的改變:
“第 1 頁(yè):
https://so.gushiwen.cn/search.aspx?value=酒第 2 頁(yè):
”https://so.gushiwen.cn/search.aspx?type=title&page=2&value=酒
其中經(jīng)過(guò)測(cè)試 type=title 可以去除,而page=2 顯然是頁(yè)碼,那么 page=1 能否獲取到第 1 頁(yè)呢?
答案是可以的,因此不需要用 requests 的 post 請(qǐng)求,直接 get 下面的 URL 就可到達(dá)指定頁(yè)面:https://so.gushiwen.cn/search.aspx?page=頁(yè)碼&value=關(guān)鍵字
大致分析完就可以寫代碼了
代碼實(shí)現(xiàn)
首先導(dǎo)入庫(kù),設(shè)置請(qǐng)求頭
import requests
from lxml import html
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
以關(guān)鍵字酒為例,嘗試獲取第一頁(yè)全部?jī)?nèi)容:
import requests
from lxml import html
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
html_data = requests.get('https://so.gushiwen.cn/search.aspx?page=1&value=酒', headers=headers).text
print(html_data)

返回的文本中有我們需要的內(nèi)容,說(shuō)明組合而成的請(qǐng)求是沒(méi)有問(wèn)題的。接下來(lái)就可以解析文本獲取具體內(nèi)容了,本文采用 Xpath:
selector = html.fromstring(html_data)
poets = selector.xpath("/html/body/div[2]/div[1]/div[@class='sons']")
for poet in poets:
title = ''.join(poet.xpath("div[1]/p[1]/a/b//text()")).strip()
print(title)

詩(shī)人和朝代被分隔至兩行,說(shuō)明之間存在換行符及空格,可以用包含.strip()的列表推導(dǎo)式去除:
for poet in poets:
title = ''.join(poet.xpath("div[1]/p[1]/a/b//text()")).strip()
source = ''.join(poet.xpath('div[1]/p[2]//text()'))
source = ''.join([i.strip() for i in source])
print(title, source)

最后是對(duì)詩(shī)句的解析。為了獲取關(guān)鍵字真正在的句子,我們要通過(guò)句號(hào)或者問(wèn)號(hào)將整首詩(shī)斷開成多個(gè)完整句:
for poet in poets:
title = ''.join(poet.xpath("div[1]/p[1]/a/b//text()")).strip()
source = ''.join(poet.xpath('div[1]/p[2]//text()'))
source = ''.join([i.strip() for i in source])
contents = ''.join(poet.xpath('div[1]/div[@class="contson"]//text()')).strip().replace('\n', '。').replace('?', '。').split('。')
print(title, source, contents)

對(duì)每一首詩(shī)逐漸判斷是否包含關(guān)鍵字:
for poet in poets:
title = ''.join(poet.xpath("div[1]/p[1]/a/b//text()")).strip()
source = ''.join(poet.xpath('div[1]/p[2]//text()'))
source = ''.join([i.strip() for i in source])
contents = ''.join(poet.xpath('div[1]/div[@class="contson"]//text()')).strip().replace('\n', '。').replace('?', '。').split('。')
content_lst = []
for i in contents:
if '酒' in i:
content = i.strip() + '。'
content_lst.append(content)
# 有的詩(shī)可能有兩句都包含關(guān)鍵字,這兩句詩(shī)就都是需求
if not content_lst: # 有可能只有題目中含有關(guān)鍵詞,這種詩(shī)就跳過(guò)
continue
for j in list(set(content_lst)): # 有可能有的詩(shī)雖然有兩句都包含關(guān)鍵字,但這兩句是一樣的,需要去重
print(j, title, source)

大部分需求已經(jīng)滿足,最后只需要利用循環(huán)結(jié)構(gòu)組裝 URL 達(dá)到范圍多頁(yè)的目的,同時(shí)關(guān)鍵字可以修改為 input 交互輸入,代碼如下:
import requests
from lxml import html
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
def poet_content(keyword,num,url):
html_data = requests.get(url, headers=headers).text
selector = html.fromstring(html_data)
poets = selector.xpath("/html/body/div[2]/div[1]/div[@class='sons']")
for poet in poets:
title = ''.join(poet.xpath("div[1]/p[1]/a/b//text()")).strip()
source = ''.join(poet.xpath('div[1]/p[2]//text()'))
source = ''.join([i.strip() for i in source])
contents = ''.join(poet.xpath('div[1]/div[@class="contson"]//text()')).strip().replace('\n', '。').replace('?','。').split('。')
content_lst = []
for i in contents:
if keyword in i:
content = i.strip() + '。'
content_lst.append(content)
if not content_lst:
continue
for j in list(set(content_lst)):
print(num, j)
print(f'<{title}>', source)
print('')
num += 1
return num
if __name__ == '__main__':
keyword = input('> 請(qǐng)輸入關(guān)鍵詞: ')
print('')
num = 1
for i in range(1, 3):
url = f'https://so.gushiwen.org/search.aspx?page={i}&value={keyword}'
num = poet_content(keyword, num, url)

至此,我們就通過(guò) Python 爬蟲就成功制作了一款“飛花令”小工具,感興趣的讀者可以自己嘗試一下!
PS:公號(hào)內(nèi)回復(fù)「Python」即可進(jìn)入Python 新手學(xué)習(xí)交流群,一起 100 天計(jì)劃!
老規(guī)矩,兄弟們還記得么,右下角的 “在看” 點(diǎn)一下,如果感覺(jué)文章內(nèi)容不錯(cuò)的話,記得分享朋友圈讓更多的人知道!


【神秘禮包獲取方式】
