5

为什么不能把div下的class值都捕捉到

html = '''
                                <div class="ee">
                                    <span class="p-price">
                                        <span>¥</span>
                                        <span class="price J-p-100003385931"></span>
                                    </span>
                                    <a class="notice J-notify-sale" data-type="1" data-sku="100003385931" href="#none" clstag="shangpin|keycount|product|jiangjia_1">降价通知</a>
                                </div>
                                <div class="ff">
                                    <span class="p-price">
                                        <span>¥</span>
                                        <span class="price J-100003385931"></span>
                                    </span>
                                    <a class="notice J-notify-sale" data-type="1" data-sku="100003385931" href="#none" clstag="shangpin|keycount|product|jiangjia_1">降价通知</a>

                                <div class="dd">
                                    <span class="p-price">
                                        <span>¥</span>
                                        <span class="price J-p-100003385931"></span>
                                    </span>
                                    <a class="notice J-notify-sale" data-type="1" data-sku="100003385931" href="#none" clstag="shangpin|keycount|product|jiangjia_1">降价通知</a>

'''

from lxml import etree
element = etree.HTML(html)
a_text = element.xpath('//a[contains(@class,"J-notify")]/../div/@class')
print(a_text)

我发现给这3个标签:
1、都填上就一个都捕捉不到。
2、都去掉就只能捕捉2个。
3、

调转也只能捕捉后2者。
4、a_text = element.xpath('//a[contains(@class,"J-notify")]/../@class') 都可以捕捉到。
请问这是为什么?
查看全部
weixin_43256057
weixin_43256057
2019/09/22 16:50
  • python
  • 点赞
  • 收藏
  • 回答
    私信

2个回复