[Python] 纯文本查看 复制代码 import requests
from bs4 import BeautifulSoup
import random
import time
for t in range(10):#更改数字即可显示几首诗
b=random.randint(0,25)#随机一页
c=random.randint(0,9)#随机一首
print(f'第{b}页,第{c+1}首')
url=f'https://www.gushiwen.cn/default_{b}.aspx'
req=requests.get(url=url).content.decode()
soup=BeautifulSoup(req,'lxml')
title=soup.select('div[class="sons"]>div>p>a>b')#标题
contson=soup.select('div[class="sons"]>div>div[class="contson"]')#诗词内容
Author=soup.select('div[class="sons"]>div[class="cont"]>p[class="source"]>a:first-child')#作者
Dynasty=soup.select('div[class="sons"]>div[class="cont"]>p[class="source"]>a:nth-child(2)')#朝代
print(f'{title[c].text}\n{Author[c].text} {Dynasty[c].text}\n{contson[c].text}')
print('***********************************************')
time.sleep(3)
|