zoukankan      html  css  js  c++  java
  • 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()

  • 相关阅读:
    Jzoj2682 最长双回文串
    Jzoj2682 最长双回文串
    【hdu3853】Loops
    【tyvj1015】【caioj1060】公路乘车
    【luogu1064】金明的预算方案
    【bzoj1260】【CQOI2007】涂色paint
    【UVa1629】Cake slicing
    【NYOJ746】整数划分(四)
    【NYOJ 15】括号匹配2
    【poj2955】Brackets
  • 原文地址:https://www.cnblogs.com/hellogiser/p/xpath_demos.html
Copyright © 2011-2022 走看看