繁体   English   中英

AttributeError:'xml.etree.ElementTree.Element'对象没有属性'encode'

[英]AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'encode'

我正在尝试制作桌面通知程序,为此我正在从网站上抓取新闻。 当我运行该程序时,我收到以下错误。

news[child.tag] = child.encode('utf8')
AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'encode'

我该如何解决? 我对此完全陌生。 我试着寻找解决方案,但没有一个能为我工作。

这是我的代码:

import requests
import xml.etree.ElementTree as ET


# url of news rss feed
RSS_FEED_URL = "http://www.hindustantimes.com/rss/topnews/rssfeed.xml"


def loadRSS():
    '''
    utility function to load RSS feed
    '''
    # create HTTP request response object
    resp = requests.get(RSS_FEED_URL)
    # return response content
    return resp.content


def parseXML(rss):
    '''
    utility function to parse XML format rss feed
    '''
    # create element tree root object
    root = ET.fromstring(rss)
    # create empty list for news items
    newsitems = []
    # iterate news items
    for item in root.findall('./channel/item'):
        news = {}
        # iterate child elements of item
        for child in item:
            # special checking for namespace object content:media
            if child.tag == '{http://search.yahoo.com/mrss/}content':
                news['media'] = child.attrib['url']
            else:
                news[child.tag] = child.encode('utf8')
        newsitems.append(news)
    # return news items list
    return newsitems


def topStories():
    '''
    main function to generate and return news items
    '''
    # load rss feed
    rss = loadRSS()
    # parse XML
    newsitems = parseXML(rss)
    return newsitems

您正在尝试将str转换为bytes ,然后将这些字节存储在字典中。 问题是你执行此操作的对象是xml.etree.ElementTree.Element ,而不是str

你可能意味着从内部或周围的元素获取文本,然后encode() 这一点 文档建议使用itertext()方法:

''.join(child.itertext())

这将评估为str ,然后您可以encode()

请注意, texttail属性可能不包含文本(强调添加):

它们的值通常是字符串, 但可以是任何特定于应用程序的对象

如果要使用这些属性,则必须处理None或非字符串值:

head = '' if child.text is None else str(child.text)
tail = '' if child.text is None else str(child.text)
# Do something with head and tail...

即使这还不够。 如果texttail包含某些意外(或错误)编码的bytes对象,则会引发UnicodeEncodeError

字符串与字节

我建议将文本保留为str ,而不是编码。 将文本编码到bytes对象是将其写入二进制文件,网络套接字或某些其他硬件之前的最后一步。

有关字节和字符之间差异的更多信息,请参阅Ned Batchelder的“ 实用Unicode,或者,我如何阻止痛苦? ”( 来自PyCon US 2012的 36分钟视频 )。 他涵盖了Python 2和3。

示例输出

使用child.itertext()方法,而不是编码字符串 ,我从topStories()获得了一个看起来很合理的词典列表:

[
  ...,
  {'description': 'Ayushmann Khurrana says his five-year Bollywood journey has '
                  'been “a fun ride”; adds success is a lousy teacher while '
                  'failure is “your friend, philosopher and guide”.',
    'guid': 'http://www.hindustantimes.com/bollywood/i-am-a-hardcore-realist-and-that-s-why-i-feel-my-journey-has-been-a-joyride-ayushmann-khurrana/story-KQDR7gMuvhD9AeQTA7tbmI.html',
    'link': 'http://www.hindustantimes.com/bollywood/i-am-a-hardcore-realist-and-that-s-why-i-feel-my-journey-has-been-a-joyride-ayushmann-khurrana/story-KQDR7gMuvhD9AeQTA7tbmI.html',
    'media': 'http://www.hindustantimes.com/rf/image_size_630x354/HT/p2/2017/06/26/Pictures/actor-ayushman-khurana_24f064ae-5a5d-11e7-9d38-39c470df081e.JPG',
    'pubDate': 'Mon, 26 Jun 2017 10:50:26 GMT ',
    'title': "I am a hardcore realist, and that's why I feel my journey "
             'has been a joyride: Ayushmann...'},
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM