python - 不需要的 python feedparser 实例化遗迹

标签 python feedparser

问题:如何终止实例化或确保我正在创建 python 通用 feedparser 的新实例化?


信息:

我现在正在开发一个程序,可以下载和编目大量博客。除了一个不幸的错误外,它运行良好。我的代码设置为获取博客网址列表并通过 for 循环运行它们。每次运行它都会选择一个 url 并将其发送到一个单独的类,该类管理数据的下载、提取和保存到文件。

第一个 url 工作正常。它会下载整个博客并将其保存到一个文件中。但是它下载的第二个博客也将包含第一个博客的所有数据,我完全不知道为什么。


代码片段:

class BlogHarvester:
  def __init__(self,folder):
    f = open(folder,'r')
    stop = folder[len(folder)-1]
    while stop != '/':
        folder = folder[0:len(folder)-1]
        stop = folder[len(folder)-1]
    blogs = []
    for line in f:
        blogs.append(line)

    for herf in blogs:
        blog = BlogParser(herf)
        sPath = ""
        uid = newguid()##returns random hash.
        sPath = uid
        sPath = sPath + " - " + blog.posts[0].author[1:5] + ".blog"
        print sPath
        blog.storeAsFile(sPath)

class BlogParser:
  def __init__(self, blogherf='null', path='null', posts = []):
    self.blogherf = blogherf

    self.blog = feedparser.parse(blogherf)
    self.path = path
    self.posts = posts
    if blogherf != 'null':
        self.makeList()
    elif path != 'null':
        self.loadFromFile()

class BlogPeices:
  def __init__(self,title,author,post,date,publisher,rights,comments):
    self.author = author
    self.title = title
    self.post = post
    self.date = date
    self.publisher = publisher
    self.rights = rights
    self.comments = comments

我包含了我认为可能有用的片段。抱歉,如果有任何令人困惑的工件。这个程序一直很痛苦。

最佳答案

问题是 posts=[]。默认参数是在编译时计算的,而不是运行时,因此对对象的更改会在类的生命周期内保留。而是使用 posts=None 并测试:

if posts is None:
  self.posts = []

关于python - 不需要的 python feedparser 实例化遗迹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2080071/

相关文章:

python - 如何修复 SSL : CERTIFICATE_VERIFY_FAILED feedparser?

node.js - 使用 Mongoose 和 FeedParser 跟踪 Node.js 中的多个回调何时完成

python - Python中使用feedparser的updated_pa​​rsed和published_pa​​rsed的区别

python - 三个数组的python点积中的数组计算

python - 以下 python 代码返回什么数据类型?

python - 使用 Hadoop Streaming 和 Python 在 Map Reduce 中进行叉积

c# - RSS.NET 无法解析 feedburner 提要

python - 在 Django 模板标签库中导入外部库时出错

python - 将一系列 xarray.DataArray 转换为 numpy 数组

python - 转换 Pandas 数据框