You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解析RSS Feed的description标签时返回None的问题排查

问题描述

访问某RSS Feed(截图显示存在description标签),但解析Feed时description标签返回None,触发如下报错:

AttributeError: 'NoneType' object has no attribute 'text'
Traceback:
File "C:\Users\User\Desktop\news-recommendation\env\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 535, in _run_script
    exec(code, module.__dict__)
File "C:\Users\User\Desktop\news-recommendation\app.py", line 66, in <module>
    data=parseRSS('https://rss.app/feeds/6BJraU9Ff0IeqC3c.xml')
File "C:\Users\User\Desktop\news-recommendation\parseRSS.py", line 23, in parseRSS
    description_content = BeautifulSoup(item.description.text, "html.parser")

使用的解析代码如下:

resp=requests.get(url)
soup = BeautifulSoup(resp.content, features="xml")
soup.prettify()
items = soup.findAll('item')

news_items = []
for item in items:
    news_item={}
    news_item['title']=item.title.text
    news_item['link']=item.link.text
    news_item['pubDate']=item.pubDate.text
    news_items.append(news_item)
 
    description_content = BeautifulSoup(item.description.text, "html.parser")
    # Remove the img tag
    img_tag = description_content.find('img')
    if img_tag:
     img_tag.decompose()
  
     # Assuming you want to keep the rest of the content as HTML
    news_item['description'] = str(description_content)
解决方案
  • 核心问题:个别item节点的description标签可能为空或缺失(即使截图显示存在,也可能存在部分条目异常),直接调用item.description.text会因item.description为None触发报错。
  • 修复步骤:
    1. 先判断item.description是否存在,再获取其文本内容
    2. 处理空值情况,给description字段设置默认值
    3. 修正代码缩进错误(原代码中if img_tag:后的逻辑缩进不正确)

修改后的代码:

resp=requests.get(url)
soup = BeautifulSoup(resp.content, features="xml")
soup.prettify()
items = soup.findAll('item')

news_items = []
for item in items:
    news_item={}
    news_item['title']=item.title.text
    news_item['link']=item.link.text
    news_item['pubDate']=item.pubDate.text
    
    # 处理description字段,避免None报错
    if item.description:
        desc_text = item.description.text.strip()
        description_content = BeautifulSoup(desc_text, "html.parser")
        # 移除img标签
        img_tag = description_content.find('img')
        if img_tag:
            img_tag.decompose()
        # 保留剩余HTML内容
        news_item['description'] = str(description_content)
    else:
        # 无description时设置默认值
        news_item['description'] = ""
    
    news_items.append(news_item)

内容的提问来源于stack exchange,提问作者NoIdea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:52:16