gpt4 book ai didi

html - 如何将 Scrapy XPath 与 XML 命名空间一起使用?

转载 作者:行者123 更新时间:2023-12-04 02:40:07 25 4
gpt4 key购买 nike

如何提取 <content:encoded> ... </content:encoded>使用来自 RSS feed 的 scrapy XPath 的内容(下面的示例)?

<?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() , 但它不起作用。

非常感谢任何帮助。

最佳答案

您必须声明并注册一个 XML 命名空间前缀:

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

文档: register_namespace()

关于html - 如何将 Scrapy XPath 与 XML 命名空间一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59777994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com