【问题标题】:How to create an object by class and get information from the list in Python?如何在 Python 中按类创建对象并从列表中获取信息?
【发布时间】:2021-05-29 20:52:24
【问题描述】:

我正在 Python 中使用类和 ElementTree

我有一个从 Yahoo XML 获取新闻的功能,包括标题、公开日期和链接,然后我将它们存储到一个列表中:

import urllib.request
import xml.etree.ElementTree as ET

def get_contents():
    url = 'https://www.yahoo.com/news/rss'

    with urllib.request.urlopen(url) as response:
        data = response.read()
        root = ET.fromstring(data)
        channel = root[0]
        titles = [nt.text for nt in channel.iter('title')]
        dates = [pd.text for pd in channel.iter('pubDate')]
        links = [nl.text for nl in channel.iter('link')]
        contents = [[titles[i], dates[i], links[i]] for i in range(len(titles) - 1)]

    return contents

我还有一个 Content 类,其中包含 init 函数来声明标题、公开日期和链接。另外,如果我创建对象,我有 str 函数来获取对象的格式字符串:

class Content():
    def __init__(self, title, link, pub_date):
        # TODO: your code here
        self.title = title
        self.link = link
        self.pub_date = pub_date
    def __str__(self):
        # TODO: your code here
        return self.title + '. (' + self.pub_date + ')' + '\n' + self.link

现在,我想通过 Content() 类创建一个对象,并通过我拥有的列表(在 get_contents() 函数返回)获取标题、公开日期和链接,例如:

感谢您的帮助。

【问题讨论】:

    标签: python list class elementtree


    【解决方案1】:

    您可以直接创建内容项,而不是创建值列表,然后将其打印出来:

    import urllib.request
    import xml.etree.ElementTree as ET
    
    class Content():
        def __init__(self, title, link, pub_date):
            self.title = title
            self.link = link
            self.pub_date = pub_date
        def __str__(self):
            return self.title + '. (' + self.pub_date + ')' + '\n' + self.link
    
    def get_contents():
        url = 'https://www.yahoo.com/news/rss'
    
        with urllib.request.urlopen(url) as response:
            data = response.read()
            root = ET.fromstring(data)
            for item in root.findall("./channel/item"):
                title = item.findtext("title")
                pubDate = item.findtext("pubDate")
                link = item.findtext("link")
                yield Content(title, link, pubDate)
    
    
    for content in get_contents():
        print(content)
    

    此外,您还可以使用诸如feedparser 之类的第三方库来解析 RSS 提要,从而节省一些工作:

    import feedparser
    def get_contents():
        url = 'https://www.yahoo.com/news/rss'
        feed = feedparser.parse(url)
        for item in feed.entries:
            yield Content(item.title, item.link, item.published)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 2022-11-04
      • 2013-07-01
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多