xpath 过滤排除某个标签

提取某个网页的所有图片,所有图片的父标签是不确定的,而需求是要排除<a>标签下的图片

<div style='background-color:#993300; text-align: center;'> 
                                <div style='text-align: center;clear: both; padding:5px 0; display: inline-block;background-color:#993300'>
                                 <table border="0" cellpadding="0" cellspacing="0"> <tbody><tr><td> <a href='http://www.aliexpress.com/item/anywhere/33051303647.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB16xF2eBGE3KVjSZFhq6AkaFXa6.jpg'></a></td><td> <a href='http://www.aliexpress.com/item/anywhere/33048238548.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB1YMhZewmH3KVjSZKzq6z2OXXaL.jpg'></a></td><td> <a href='http://www.aliexpress.com/item/anywhere/33044721080.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB1DhJ2eBGE3KVjSZFhq6AkaFXaY.jpg'></a></td><td> <a href='http://www.aliexpress.com/item/anywhere/33014377031.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB1qkl6eBGw3KVjSZFDq6xWEpXak.jpg'></a></td><td> <a href='http://www.aliexpress.com/item/anywhere/32990955163.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB1KQpZevWG3KVjSZFPq6xaiXXat.jpg'></a></td></tr><tr><td> <a href='http://www.aliexpress.com/item/anywhere/33026321971.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB1Kb85eBGw3KVjSZFwq6zQ2FXaa.jpg'></a></td><td> <a href='http://www.aliexpress.com/item/anywhere/33045517107.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB1oQB2eAWE3KVjSZSyq6xocXXa3.jpg'></a></td><td> <a href='http://www.aliexpress.com/item/anywhere/33003023746.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB14V01eB1D3KVjSZFyq6zuFpXaD.jpg'></a></td><td> <a href='http://www.aliexpress.com/item/anywhere/32990891682.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB1YcBZewaH3KVjSZFjq6AFWpXa1.jpg'></a></td><td> <a href='http://www.aliexpress.com/item/anywhere/33049126971.html'><img style='margin: 1px; border: 0px currentColor; ' src='https://ae01.alicdn.com/kf/HTB1y7J8eqWs3KVjSZFxq6yWUXXaV.jpg'></a></td></tr> </tbody></table> 
</div>
<p> <img alt="PZH80705592_20180705025311897" src="https://ae01.alicdn.com/kf/HTB1kn6eMgHqK1RjSZFgq6y7JXXa0.jpg"><img alt="PZH80705592WHL_20180705025822366" src="https://ae01.alicdn.com/kf/HTB1.pLoMkvoK1RjSZFwq6AiCFXaR.jpg"><img alt="PZH80705592BUL_20180705025911250" src="https://ae01.alicdn.com/kf/HTB13KvBMhnaK1RjSZFBq6AW7VXaO.jpg"><img alt="PZH80705592BUL_20180705025911407" src="https://ae01.alicdn.com/kf/HTB1KrjcMbvpK1RjSZPiq6zmwXXaC.jpg"><img alt="PZH80705592BUL_20180705025912815" src="https://ae01.alicdn.com/kf/HTB16zHcMXYqK1RjSZLeq6zXppXaF.jpg"><img alt="PZH80705592WHL_2018070502294261" src="https://ae01.alicdn.com/kf/HTB1I1DBMhnaK1RjSZFBq6AW7VXa4.jpg"><img alt="PZH80705592WHL_20180705022942196" src="https://ae01.alicdn.com/kf/HTB1liYfMkvoK1RjSZFDq6xY3pXae.jpg"><img alt="PZH80705592WHL_20180705022942378" src="https://ae01.alicdn.com/kf/HTB1.pLoMkvoK1RjSZFwq6AiCFXaR.jpg"></p>

方法:

img_desc = response.xpath('//*[name(.)!="a"]/img/@src').extract() # 利用name()方法

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值