scrapy css选择器链如何表示当前节点?

时间:2017-08-30 09:14:59

标签: python css scrapy

e.g。鉴于此url

我想得到卖家的div

使用Xpath,首先我使用css获取特定卖家行,然后使用' ./'引用当前节点,然后我可以得到(5)紧跟在我的目标div(行)之后的div。

b = response.css('div#olpOfferList div.a-row.a-spacing-mini.olpOffer')[1].xpath('./div')

b的结果:

<Selector xpath='./div' data=u'<div class="a-column a-span2 olpPriceCol'>
<Selector xpath='./div' data=u'<div class="a-column a-span3 olpConditio'>
<Selector xpath='./div' data=u'<div class="a-column a-span3 olpDelivery'>
<Selector xpath='./div' data=u'<div class="a-column a-span2 olpSellerCo'>
<Selector xpath='./div' data=u'<div class="a-column a-span2 olpBuyColum'>

但是对于css,我没有选择当前节点后如何引用当前节点

a = response.css('div#olpOfferList div.a-row.a-spacing-mini.olpOffer')[1].css('div > div')

结果:

<Selector xpath=u'descendant-or-self::div/div' data=u'<div class="a-column a-span2 olpPriceCol'>
<Selector xpath=u'descendant-or-self::div/div' data=u'<div class="a-column a-span3 olpConditio'>
<Selector xpath=u'descendant-or-self::div/div' data=u'<div class="a-section a-spacing-small">\n'>
<Selector xpath=u'descendant-or-self::div/div' data=u'<div class="a-column a-span3 olpDelivery'>
<Selector xpath=u'descendant-or-self::div/div' data=u'<div class="a-column a-span2 olpSellerCo'>
<Selector xpath=u'descendant-or-self::div/div' data=u'<div class="a-column a-span2 olpBuyColum'>
<Selector xpath=u'descendant-or-self::div/div' data=u'<div class="a-button-stack">\n           '>

我尝试了这个,但收到了错误:

a = response.css('div#olpOfferList div.a-row.a-spacing-mini.olpOffer')[1].css('> div')

如何用css引用当前节点? :D谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

如果最后没有XPATH选择器,你就无法做到这一点。

所以你使用的是正确的

b = response.css('div#olpOfferList div.a-row.a-spacing-mini.olpOffer')[1].xpath('./div')

由于css无法引用当前节点或父节点,因此您必须仅使用XPath。