xpath 总结

 <table id="MatchTable">

<tr id="Explain_1228761" style="display:none;color:green;" class="ni"><td colspan="14"></td></tr>

<tr id="row_1716002" class="ni2" name="2016-04-12" gamename="欧冠杯" polygoal="-2" matchid="1716002"> hello</tr>

</table>

 

选择selector

1)根据属性值选择

//*[@id="MatchTable"]

table = response.xpath('//*[@id="MatchTable"]')

2)根据是否具有某个属性选择元素

tr[@matchid]

tr = table.xpath('tr[@matchid]')

 

3)选择内容

<a href="http://cms.8win.com/zybl/one-201604236003"> HelloWorld</a>

3.1)选择text

selector.xpath('text').extract_first()  # HelloWorld

3.2)选择href的text

selector.xpath('@href').extract_first() # http://cms.8win.com/zybl/one-201604236003

 

4)css选择

<p class="abstract">
飓风主力中场弗里茨勒(9场1球)本轮累计黄牌禁赛。(一路追球)
</p>

 

<p class="abstract abstract-nophoto">
飓风解放者杯小组成功出线,但联赛最近有所起伏,他们的近3个客场没有胜绩(1平2负),目前排名B组第4,已经被榜首球队拉开了8分差距。(一路追球)
</p>

对应代码

div.xpath('p[starts-with(@class,"abstract")]/text()').extract_first().strip()

div.xpath('p[contains(@class,"abstract")]/text()').extract_first().strip()

 

转载于:https://www.cnblogs.com/hellogiser/p/xpath_demos.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值