例外:' NoneType'对象没有属性' text'

时间:2018-06-18 08:54:23

标签: python-3.x

我正在尝试解析以下页面:https://www.youtube.com/feeds/videos.xml?channel_id=UCJWCJCWOxBYSi5DhCieLOLQ以获取以下所有yt:video id元素,例如并将它们保存到数组/列表中,即:

<yt:videoId>_V0vqy046YM</yt:videoId>

...

<yt:videoId>_V1vqy046YM</yt:videoId>

...

<yt:videoId>_V2vqy046YM</yt:videoId>

但是我一直收到以下错误消息,我该如何解决?

Exception: 'NoneType' object has no attribute 'text'

到目前为止,这是我的代码:

try:
    recent_video_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=' + channel_id
    print('Querying:', recent_video_url)
    recent_video_response = requests.get(recent_video_url)
    recent_video_response_data = recent_video_response.content
    root = tree.fromstring(recent_video_response_data)
    for child in root.findall('{http://www.w3.org/2005/Atom}entry'):
        print(child.find('yt:videoId').text)
except Exception as e:
    raise Exception(e)

1 个答案:

答案 0 :(得分:1)

import requests
import xml.etree.ElementTree as Et


try:
    channel_id = 'UCJWCJCWOxBYSi5DhCieLOLQ'
    recent_video_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=' + channel_id

    print('Querying:', recent_video_url)
    recent_video_response = requests.get(recent_video_url)

    tree = Et.ElementTree(Et.fromstring(recent_video_response.content))
    namespaces = {'yt': 'http://www.youtube.com/xml/schemas/2015'}

    recent_video_response_data = [element.text for element in tree.findall('.//yt:videoId', namespaces=namespaces)]
    print(recent_video_response_data)
except Exception as e:
    raise Exception(e)