【问题标题】:feedparser cant get youtube playerfeedparser 无法获取 youtube 播放器
【发布时间】:2012-10-29 16:35:16
【问题描述】:

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

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

我的代码很简单:

d = feedparser.parse('http://feeds.feedburner.com/NotciasPs3evita-Mypst')

这返回(摘录):

 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'}},

除了 youtube 播放器 &lt;object&gt; 标记外,一切都准备就绪。这是 feedparser 错误还是我的 rss 上的问题? python 上还有其他库可以做到这一点吗?

【问题讨论】:

    标签: python feedparser


    【解决方案1】:

    默认情况下会去除 feedparser sanitizes HTML input&lt;object&gt;&lt;param&gt;&lt;embed&gt; 标签。

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

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

    feedparser.SANITIZE_HTML = False
    

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

    _HTMLSanitizer.acceptable_elements.update(['object', 'param', 'embed'])
    

    这两种方法都有固有的风险,并且您正在以这种方式受到攻击。我使用的方法是完全切换清理,然后使用some other method 清理HTML,可能使用带有白名单的lxml.html.clean 并在host_whitelist 中列出YouTube。

    【讨论】:

    • 实际上是feedparser._HTMLSanitizer.acceptable_elements.update(['object', 'param', 'embed'])feedparser._HTMLSanitizer.acceptable_attributes.update(['allowfullscreen', 'frameborder']) 也是如此。我想知道为什么我在 Feedparser 的文档中找不到此信息。
    【解决方案2】:

    作为对上述解决方案的响应,似乎最近 SANITIZE_HTML 设置已移至 feedparser.api 下:

    feedparser.api.SANITIZE_HTML = False
    feedparser.api.mixin.SANITIZE_HTML = False
    

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 2015-04-03
      • 2015-09-25
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 2014-07-13
      相关资源
      最近更新 更多