菜单
  • 字节-豆包AI
  • 阿里-通义AI
  • 讯飞-星火AI
  • 百度-文心AI
  • GPT-代码AI
  • 写作助手
  • 论文助手

import requests from bs4 import beautifulsoup def get_poems(): url = "https://so.gushiwen.cn/gushi/tangshi.aspx" response = requests.get(url) response.encoding = "utf-8" soup = beautifulsoup(response.text, "html.parser") # 获取所有的诗列表 poem_list = soup.find_all("div", class_="typecont") poems = [] # 遍历诗列表,提取相关信息 for poem in poem_list: # 获取诗类型 poem_type = poem.find_previous_sibling("div", class_="bookml").find("span").text # 获取诗题目

poem_title = poem.find("b").text # 获取诗作者 poem_author = poem.find("p", class_="source").text.strip() # 获取诗内容 poem_content = poem.find("div", class_="contson").text.strip() # 创建诗对象,添加到诗列表中 poem_obj = { "type": poem_type, "title": poem_title, "author": poem_author, "content": poem_content } poems.append(poem_obj) return poems [2024-06-24 00:59:22 | GPT-代码助手 | 316字解答]

相关提问