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

Pygooglenews导入失败的解决办法求助

Pygooglenews导入失败的解决办法求助

问题描述

用户当前安装的依赖版本:

Name: pygooglenews
Version: 0.1.3
Name: feedparser
Version: 6.0.11

尝试导入pygooglenews时触发如下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-7a5a1e5c2c1c> in <module>
----> 1 from pygooglenews import GoogleNews

~/venv/lib/python3.9/site-packages/pygooglenews/__init__.py in <module>
----> 1 import feedparser
      2 from bs4 import BeautifulSoup
      3 import urllib
      4 from dateparser import parse as parse_date
      5 import requests

~/venv/lib/python3.9/site-packages/feedparser.py in <module>
     91 else:
     92     # Python 3.1 deprecates decodestring in favor of decodebytes
---> 93     _base64decode = getattr(base64, 'decodebytes', base64.decodestring)
     94 
     95 # _s2bytes: convert a UTF-8 str to bytes if the interpreter is Python 3

AttributeError: module 'base64' has no attribute 'decodestring'

求助:有没有可行的解决办法?


解决方案

这个问题的根源是feedparser 6.0.11与你使用的Python版本存在兼容性问题:Python 3.10及以上版本已经移除了base64.decodestring方法,而该版本的feedparser兼容代码没有正确适配这个变更。给你几个实用的解决思路:

  • 优先升级feedparser到最新版:新版feedparser已经修复了这个兼容性bug,执行以下命令即可完成升级:

    pip install --upgrade feedparser
    
  • 同步升级pygooglenews:如果升级feedparser后仍有问题,可能是旧版pygooglenews(0.1.3)和新版feedparser存在依赖冲突,试试同步升级pygooglenews:

    pip install --upgrade pygooglenews
    
  • 手动修改feedparser代码(应急方案):如果暂时无法升级依赖,可以直接修改报错的feedparser文件:
    找到虚拟环境中feedparser.py的路径(比如你这里的~/venv/lib/python3.9/site-packages/feedparser.py),定位到第93行的代码:

    _base64decode = getattr(base64, 'decodebytes', base64.decodestring)
    

    将其修改为:

    _base64decode = base64.decodebytes
    

    因为Python 3.1+已经统一使用decodebytes替代decodestring,直接使用该方法就能解决报错。

备注:内容来源于stack exchange,提问作者OJT

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 13:14:38