我这样做:
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')