用Python获取古诗文网的完整古诗
要获取古诗文网的完整古诗,可以使用Python请求库(如requests)发起HTTP请求,然后使用解析库(如BeautifulSoup)解析HTML页面,提取古诗的相关信息。
以下是一个示例代码,可以获取古诗文网中某一首古诗的标题、作者和内容:
import requests
from bs4 import BeautifulSoup
# 古诗文网中某一首古诗的URL
url = 'https://www.gushiwen.org/shiwen/default_2A35A35A1.aspx'
# 发起HTTP GET请求
response = requests.get(url)
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 提取古诗的标题、作者和内容
title = soup.find('h1').text
author = soup.find('p', class_='source').find('a').text
content = soup.find('div', class_='contson').text.strip()
# 打印古诗的标题、作者和内容
print(f'标题:{title}')
print(f'作者:{author}')
print(f'内容:{content}')
请注意,古诗文网的网页结构可能会有变化,以上示例代码仅适用于特定的古诗页面。如果要获取多首古诗,可以通过修改URL中的参数实现。同时,也可以编写循环来批量获取多首古诗的信息。
原文地址: https://www.cveoy.top/t/topic/i58T 著作权归作者所有。请勿转载和采集!