feedparser 无法获得 youtube 播放器

fab*_*ols 1 python feedparser

我正在测试feedparser我的 RSS 提要。它就像一个魅力,我得到了所有的条目。

有些新闻有一个内嵌的 youtube 播放器,但这只是没有出现在 feedparser 的返回值中。

我的代码很简单:

d = feedparser.parse('http://feeds.feedburner.com/NotciasPs3evita-Mypst')
Run Code Online (Sandbox Code Playgroud)

这将返回(摘录):

 guidislink': False,
          'id': u'http://mypst.com.br/forum/index.php?/topic/17336-gamegen-call-of-duty-black-ops-2-ganha-trailer-com-acao-real-e-muitas-surpresas/',
          'link': u'http://mypst.com.br/forum/index.php?/topic/17336-gamegen-call-of-duty-black-ops-2-ganha-trailer-com-acao-real-e-muitas-surpresas/',
          'links': [{'href': u'http://mypst.com.br/forum/index.php?/topic/17336-gamegen-call-of-duty-black-ops-2-ganha-trailer-com-acao-real-e-muitas-surpresas/',
                     'rel': u'alternate',
                     'type': u'text/html'}],
          'published': u'Mon, 29 Oct 2012 14:53:58 +0000',
          'published_parsed': time.struct_time(tm_year=2012, tm_mon=10, tm_mday=29, tm_hour=14, tm_min=53, tm_sec=58, tm_wday=0, tm_yday=303, tm_isdst=0),
          'summary': u'A Activision revelou hoje um novo trailer de Call of Duty: Black Ops 2, substituindo as cenas de a\xe7\xe3o do jogo por cenas de a\xe7\xe3o na vida real. O trailer traz diversas \u201csurpresas\u201d e alguns zumbis.<br />\n<br />\n<br />\n<br />\nCall of Duty: Black Ops 2 chegar\xe1 no dia 13 de novembro nos Estados Unidos.<br />\n<br />\n<br />\n<em class="bbc"><strong class="bbc">Fonte: <a class="bbc_url" href="http://www.gamegen.com.br/playstation3/call-of-duty-black-ops-2-ganha-trailer-com-acao-real-e-muitas-surpresas/" rel="nofollow external" title="Link externo">GameGeneration</a></strong></em>',
          'summary_detail': {'base': u'http://feeds.feedburner.com/NotciasPs3evita-Mypst',
                             'language': None,
                             'type': u'text/html',
                             'value': u'A Activision revelou hoje um novo trailer de Call of Duty: Black Ops 2, substituindo as cenas de a\xe7\xe3o do jogo por cenas de a\xe7\xe3o na vida real. O trailer traz diversas \u201csurpresas\u201d e alguns zumbis.<br />\n<br />\n<br />\n<br />\nCall of Duty: Black Ops 2 chegar\xe1 no dia 13 de novembro nos Estados Unidos.<br />\n<br />\n<br />\n<em class="bbc"><strong class="bbc">Fonte: <a class="bbc_url" href="http://www.gamegen.com.br/playstation3/call-of-duty-black-ops-2-ganha-trailer-com-acao-real-e-muitas-surpresas/" rel="nofollow external" title="Link externo">GameGeneration</a></strong></em>'},
          'title': u'[GameGen] Call of Duty: Black Ops 2 ganha trailer com a\xe7\xe3o real e muitas surpresas',
          'title_detail': {'base': u'http://feeds.feedburner.com/NotciasPs3evita-Mypst',
                           'language': None,
                           'type': u'text/plain',
                           'value': u'[GameGen] Call of Duty: Black Ops 2 ganha trailer com a\xe7\xe3o real e muitas surpresas'}},
Run Code Online (Sandbox Code Playgroud)

除了 youtube 播放器<object>标签外,一切都已就绪。这是 feedparser 错误还是我的 rss 上的问题?python 上还有其他库可以做到这一点吗?

Mar*_*ers 5

feedparser 会清理 HTML 输入<object><param>并且<embed>默认情况下会去除标签。

您需要禁用清理(仅当您真正信任来源时),或将 YouTube 标签列入白名单。

要禁用清理,请设置SANITIZE_HTML为 False:

feedparser.SANITIZE_HTML = False
Run Code Online (Sandbox Code Playgroud)

要添加到白名单,请将元素添加到_HTMLSanitizer.acceptable_elements集合中:

_HTMLSanitizer.acceptable_elements.update(['object', 'param', 'embed'])
Run Code Online (Sandbox Code Playgroud)

这两种方法都有固有的风险,而您正在以这种方式向攻击敞开大门。我使用的方法是完全切换清理,然后使用其他一些方法清理 HTML,可能使用lxml.html.clean白名单并在host_whitelist.