zoukankan      html  css  js  c++  java
  • xpath语法

    基础命令

    hxs = response.xpath('//a')         # 子子孙孙的 【a标签】      # 标签
    hxs = response.xpath('//a[2]')      # 无结果                    # 标签
    hxs = response.xpath('//a[@id]')    # 所有含有id值的【a标签】   # 标签
    hxs = response.xpath('//a/@id')     # 所有a标签的【id值】       # 属性值
    hxs = response.xpath('//a[@id="i1"]')   # id值=1的【a标签】     # 标签
    hxs = response.xpath('//a[@href="link.html"][@id]')         # 逻辑与+标签
    hxs = response.xpath('//a[contains(@href, "link")]')        # 包含+标签
    hxs = response.xpath('//a[starts-with(@href, "link")]')     # 开头+标签
    hxs = response.xpath('//a[re:test(@id, "id+")]')           # 正则+标签
    hxs = response.xpath('//a[re:test(@id, "id+")]/text()')    # 正则+文本
    hxs = response.xpath('//a[re:test(@id, "id+")]/@href')     # 正则+属性值
    hxs = response.xpath('/html/body/ul/li/a/@href')            # 属性值
    hxs = response.xpath('//body/ul/li/a/@href')                # 属性值
    
    # print(hxs)                # xpath对象
    # print(hxs.extract())        # 解析所有
    # print(hxs.extract_first())  # 解析第一个

    循环

    ul_list = Selector(response=response).xpath('//body/ul/li')
    for item in ul_list:
        print(item.extract())
        # v = item.xpath('./a/span')        # 1
        # v = item.xpath('a/span')          # 2
        v = item.xpath('.//span')             # 3
        print(v.extract())
  • 相关阅读:
    php odbc连接 查询显示不完整问题
    php集成环境
    intent实现网页跳转
    夜神模拟器
    Android编程知识点3-Intent
    Android编程知识点2- 线性布局,随机数
    Android编程知识点1-Button,ListView
    数据存储和访问
    Android计时器
    组件通信2
  • 原文地址:https://www.cnblogs.com/sunch/p/10764067.html
Copyright © 2011-2022 走看看