Как использовать Scrapy XPath с XML пространствами имен? - PullRequest
2 голосов
/ 17 января 2020

Как извлечь <content:encoded> ... </content:encoded> контент с использованием scrapy XPath из RSS-канала (образец ниже)?

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Latest &#8211; Reason.com</title>
    <item>
        <pubDate>Thu, 16 Jan 2020 21:40:23 +0000</pubDate>
        <content:encoded><![CDATA[<p><span style="font-weight: 400">
          Jimmy Meders was scheduled to die by lethal injection today, 
          but the Georgia parole board has granted him clemency.</span></p>]]> 
        </content:encoded>
...

Я пытался response.xpath('//content:encoded').get(), но он не работает.

Любая помощь высоко ценится.

1 Ответ

3 голосов
/ 17 января 2020

Вы должны объявить и зарегистрировать XML префикс пространства имен:

response.selector.register_namespace('content', 
                                     'http://purl.org/rss/1.0/modules/content/')
response.xpath('//content:encoded').getall()

Документация: register_namespace ()

...