将上述代码解决AttributeError: 'NoneType' object has no attribute 'find_all'问题后的正确代码输出
时间: 2024-12-17 18:13:07 浏览: 10
如果在处理过程中遇到`AttributeError: 'NoneType' object has no attribute 'find_all'`的问题,说明`poem`可能是`None`,这意味着当前迭代的位置可能没有找到预期的诗歌。在这种情况下,你需要先检查`poem`是否存在再继续操作。以下是修改后的代码:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = "https://so.gushiwen.cn/gushi/tangshi.aspx"
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
poems_list = soup.find('div', class_='list poems')
data = {
'标题': [],
'作者': [],
'朝代': [],
'正文': []
}
for poem in poems_list:
if poem and poem.name == 'div' and 'poem' in poem['class']:
title = poem.h3.text.strip() if poem.h3 else ""
author = poem.p.find('span', {'class': 'author'}).text.strip() if poem.p and poem.p.find('span', {'class': 'author'}) else ""
dynasty = poem.p.find('span', {'class': 'time'}).text.strip() if poem.p and poem.p.find('span', {'class': 'time'}) else ""
try:
content = poem.find('p').text.strip() if poem.find('p') else ""
except AttributeError:
content = ""
data['标题'].append(title)
data['作者'].append(author)
data['朝代'].append(dynasty)
data['正文'].append(content)
df = pd.DataFrame(data)
df.to_csv("古诗词.csv", index=False)
print("数据已成功保存到CSV文件。")
阅读全文