从另一个选择器创建一个选择器



当我使用xpath从另一个选择器创建选择器时,创建的选择器仍然包含完整的原始选择器内容。参见示例

original_document = """
<a>
<b>
<c>hello_1</c>
</b>
<c>hello_2</c>
</a>
"""
document_sel = scrapy.Selector(text = original_document)
second_sel = document_sel.xpath('//b')

second_sel已从原始文档中正确提取和子集:

print second_sel.extract()
[u'<b>n        <c>hello_1</c>n    </b>']

但当我试图从second_sel:中提取时

print second_sel.xpath('//c').extract()
[u'<c>hello_1</c>', u'<c>hello_2</c>']

为什么提取"hello_2"?

基于报废文档:https://doc.scrapy.org/en/latest/topics/selectors.html在"使用相对XPaths"中,必须使用".//"检索

print second_sel.xpath('.//c').extract()
[u'<c>hello_1</c>']

最新更新