如何停止 Scrapy 选择器用 html 包装 xml



我这样做:

xmlstr="<root><first>info</first></root>"
res = Selector(text=xmlstr).xpath('.').getall()
print(res)

输出为:

['<html><body><root><first>info</first></root></body></html>']

如何停止选择器用 html 和正文包装 xml?谢谢

刮擦。选择器假定 html,但需要一个type参数来更改它。

type定义了选择器类型,它可以是 "html""xml"None(默认(。

如果type None,则选择器会根据response类型自动选择最佳类型(见下文(,或者默认为"html",以防与文本一起使用。

因此,要制作 xml 选择器,只需使用 Selector(text=xmlstr, type='xml')

最新更新