python - 如何停止 Scrapy Selector 用 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 和 body 包装 xml?谢谢
解决方案
scrapy.Selector假定为 html,但需要一个type
参数来改变它。
type
定义选择器类型,它可以是"html"
,"xml"
或None
(默认)。如果
type
是None
,选择器会根据类型自动选择最佳类型response
(见下文),或者默认为"html"
以防它与文本一起使用。
所以,要制作一个 xml 选择器,只需使用Selector(text=xmlstr, type='xml')
推荐阅读
- excel - 使用vba查找和替换excel中的值 - 查找表被覆盖
- python - 我收到无法识别“行为”的错误,但我安装了它(BDD)
- r - 使用R从字符串中提取字符和数字
- firebase - 如何在与 Firestore 的实时群聊中处理数千条消息?
- telegram-bot - 电报警报操作的 Splunk 问题:无法配置操作
- airflow - Airflow 被许多用户使用,很可能其他人也有类似的问题,您可以轻松找到解决问题的方法
- php - if 条件下参数号无效的问题
- laravel - 如何在 laravel 控制器的 Blade.php 文件中显示 svg 图像
- reactjs - 如何使用 redux 离线和 redux 工具包处理多种副作用?
- pandas - 如何解决错误“未知选项:--zipfile,fileb://panda_layer.zips)”?aws,层,λ