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 | AI写代码神器 | 316点数解答]
相关提问
- 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 # 获取诗题目(316点数解答 | 2024-06-24 00:59:22)210
- 任务一:采集唐诗三百首内容 python import requests from bs4 import beautifulsoup import pandas as pd url = "https://so.gushiwen.cn/gushi/tangshi.aspx" response = requests.get(url) soup = beautifulsoup(response.text, "html.parser") poems = [] for poem in soup.find_all("div", class_="item"): title = poem.find("h1").text.strip() content = poem.find("p").text.strip() author = poem.find("span", class_="author").text.strip() poem_type = "唐诗" poems.append((15点数解答 | 2024-06-24 15:46:11)258
- import requests from bs4 import beautifulsoup import pandas as pd url = "https://so.gushiwen.cn/gushi/tangshi.aspx" response = requests.get(url) soup = beautifulsoup(response.text, 'html.parser') # 提取所需数据 poems_data = [] for row in soup.find_all('tr', class_='tlist')[1:]: cols = row.find_all('td') type_ = cols.text.strip() title = cols.a.text.strip() content = cols.text.replace('\n', '').replace('\t', '') author = cols.text.strip() poems_data.append([type_, title, conte(120点数解答 | 2024-06-24 02:04:56)243
- import requests from bs4 import beautifulsoup # 爬取唐诗三百首的内容 def scrape_data(): url = "https://so.gushiwen.cn/gushi/tangshi.aspx" headers = { "user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/89.0.4389.82 safari/537.36" } response = requests.get(url, headers=headers) if response.status_code == 200: soup = beautifulsoup(response.text, "html.parser") content_divs = soup.find_all('div', c(443点数解答 | 2024-06-24 01:16:00)247
- 为了完成上述任务,我将分步骤提供代码。首先,我会提供任务一的代码,用于从网站采集唐诗三百首的内容并保存到txt文件中。 ### 任务一:采集唐诗三百首内容 ```python import requests import re # 定义爬取函数 def fetch_poems(url): response = requests.get(url) poems_info = re.findall( r'<a href="(.*?)"\s*target="_blank" title="(.*?)">\s*<span\s*style="font-size:16px;">(.*?)</span>\s*</a>', response.text, re.s ) poems = [] for info in poems_info: poem_url = "https://so.gushiwen.cn" + info[0] poem_response = requests(208点数解答 | 2024-06-23 11:07:39)213
- def fill_and_submit_form(row): print("填充表单并提交...") wait = webdriverwait(driver, 5) # 增加等待时间 try: product_name_input = wait.until(ec.presence_of_element_located((by.xpath, '//*[@id="wcontentpanel"]/div[3]/div/div[2]/form/div[1]/div[3]/div/div/div[1]/input'))) product_category_dropdown = wait.until(ec.element_to_be_clickable((by.xpath, '//*[@id="wcontentpanel"]/div[3]/div/div[2]/form/div[1]/div[4]/div/div/div/span/span/div/div[1]'))) time.sleep(2) # 等待完成(361点数解答 | 2024-11-11 21:56:42)197
- python代码 获取到 怎么样变成 \xa0 怎么用让他的 \ 不是转义输出,为什么 import html from bs4 import BeautifulSoup html_content = "压缩天然气储运,减压,燃烧都在严格的密封状态下进行,不易发生泄露。 另外其储气瓶经过各种特殊的破坏性试验,安全可靠。" soup = BeautifulSoup(html_content, "html.parser") text = soup.get_text() print(text) 里面的文本 是 为什么 输出的不是\xa0 而是空格 ,可是我要输出\xa0,要的是直接输出 不是替换(371点数解答 | 2025-04-16 17:09:02)124
- python代码 获取到 怎么样变成 \xa0 怎么用让他的 \ 不是转义输出,为什么 import html from bs4 import BeautifulSoup html_content = "压缩天然气储运,减压,燃烧都在严格的密封状态下进行,不易发生泄露。 另外其储气瓶经过各种特殊的破坏性试验,安全可靠。" soup = BeautifulSoup(html_content, "html.parser") text = soup.get_text() print(text) 里面的文本 是 为什么 输出的不是\xa0 而是空格 ,可是我要输出\xa0(210点数解答 | 2025-04-16 17:09:53)128
- 编写一个爬虫实现深度优先爬虫,爬取的网站为 www.baidu.com。使其输出预期为:Add the seeds url ['http://www.baidu.com'] to the unvisited url list Pop out one url "http://www.baidu.com" from unvisited url list Get 10 new links Visited url count: 1 Visited deepth: 1 10 unvisited links: Pop out one url "http://news.baidu.com" from unvisited url list Get 52 new links Visited url count: 2 Visited deepth: 2 Pop out one url "http://www.hao123.com" from unvisited url list Get 311 new links Visited url count: 3 Visited deepth: 2 Pop out(2142点数解答 | 2025-05-13 15:54:49)162
- import openpyxl import smtplib import imaplib import email import json import os import re from bs4 import BeautifulSoup from fastapi import FastAPI, Form from openpyxl.styles import Alignment from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.header import decode_header from email import encoders import pandas as pd app = FastAPI() RECEIVER_EMAILS = { 0: "yundongshijie001@protonmail.com", 1: "xiaobudian001@protonmail.com" } email_address = "(182点数解答 | 2025-04-12 00:49:09)142
- 从网站“https://so.gushiwen.cn/gushi/tangshi.aspx”中采集唐诗三百首的内容。具体要求如下:1. 采集数据完整,内容规范(即不包含特殊符号,如换行符、制表符等)。2. 结果数据保存为txt格式的文本文件,每一行包含一首古诗,具体包括信息:诗类型、诗题目、诗内容、诗作者;3. 代码逻辑结构清晰,语法规范。任务二 文本特征向量化 基于任务一的结果数据,针对“诗内容”,完成文本特征向量化(tf-idf值)输出。具体要求如下:1. 数据完整,内容规范(即数值化);2. 结果数据保存为txt格式的文本文件,每一行包含一首古诗,具体包括信息:诗类型、诗题目、诗内容、诗作者、文本特征向量(tf-idf值, 多列);3. 代码逻辑结构清晰,语法规范。任务三 数据统计分析 基于任务一的结果数据,完成诗词统计。具体要求如下:1. 统计每种诗类型的诗数量,结果数据保存为excel文件;2. 统计每个作者的诗数量,结果数据保存为excel文件;3. 对诗内容进行中文分词,并输出词云图;(1159点数解答 | 2024-06-23 11:09:38)209
- 从网站“https://so.gushiwen.cn/gushi/tangshi.aspx”中采集唐诗三百首的内容。具体要求如下:1. 采集数据完整,内容规范(即不包含特殊符号,如换行符、制表符等)。2. 结果数据保存为txt格式的文本文件,每一行包含一首古诗,具体包括信息:诗类型、诗题目、诗内容、诗作者;3. 代码逻辑结构清晰,语法规范。任务二 文本特征向量化 基于任务一的结果数据,针对“诗内容”,完成文本特征向量化(tf-idf值)输出。具体要求如下:1. 数据完整,内容规范(即数值化);2. 结果数据保存为txt格式的文本文件,每一行包含一首古诗,具体包括信息:诗类型、诗题目、诗内容、诗作者、文本特征向量(tf-idf值, 多列);3. 代码逻辑结构清晰,语法规范。任务三 数据统计分析 基于任务一的结果数据,完成诗词统计。具体要求如下:1. 统计每种诗类型的诗数量,结果数据保存为excel文件;2. 统计每个作者的诗数量,结果数据保存为excel文件;3. 对诗内容进行中文分词,并输出词云图;(2078点数解答 | 2024-06-23 11:15:17)198