zoukankan      html  css  js  c++  java
  • Xpath解析

    import requests 
    from lxml import etree
    url = 'https://www.huawei.com/cn/?ic_medium=direct&ic_source=surlent'
    response = requests.get(url = url)
    ht = response.text
    tree = etree.HTML(ht)
    #也可以解析本地文件
    #tree = etree.parse(文件名)              
    
    • ‘//’表示若干层(包括一层)嵌套下, ‘/’表示一层嵌套。
    • 索引从1开始。
    • 以标签结束,则返回元素为内存地址的一个列表。
    • 取文本:/text() 取属性:/@attr 例如:/@href
    • 多重限定条件 a [@href="xxx" and @class = "xxx"]
    • 模糊匹配 div[contains(@class,'xx')] 匹配类名中含有‘xx'的div标签
    • div[starts-with(@class,'xx')] 匹配类名以‘xx'开始的div标签
    • "|"表示或 //img[@class= 'icon-image']/@alt | //img[@class= 'icon-image']/@src
    • "*“ 表示所有,例如:// *[@class = 'xxx'] 匹配所有类名为‘xxx'标签
    print(tree.xpath('//div[@class="col-sm-4 external-link"]//p')) 
    
    #[<Element p at 0x38dc350>, <Element p at 0x38dc800>, <Element p at 0x38dc828>, <Element p at 0x38dc850>, <Element p at 0x38c6e18>, <Element p at 0x38c6dc8>] 
    
    #取文本
    print(tree.xpath('//div[@class="col-sm-4 external-link"]//p/text()'))
    #['手机', '笔记本', '平板', '穿戴 ', '配件', '智能家居']
    
    print(tree.xpath('//div[@class="col-sm-4 external-link"][1]//p/text()'))
    #['手机']
    
    #取属性
    print(tree.xpath('//div[@class="col-sm-4 external-link"]/a/@href'))
    
    
    #多重限定条件 
    print(tree.xpath('//a[@href="javascript:;" and @class = "navbar-toggle "] /text()'))
    #
    ['
                                                手机、笔记本&平板...
                                                ', '
                                                解决方案&服务
                                                ']
    
    #模糊匹配
    print(tree.xpath('//div[starts-with(@id,"hw1_g")] //span//text()'))
    #['前往 ', 'consumer.huawei.com', ' 查看全部产品', '访问 ', '技术支持中心', '前往 ', 'e.huawei.com', ' 查看全部企业业务', '前往', '技术支持中心', '前往 ', 'carrier.huawei.com', ' 查看全部运营商业务', '前往', '技术支持中心', '查看全部华为云服务', 'www.huaweicloud.com', '前往 ', '帮助中心']
    
    

    # |
    url = 'https://www.coolapk.com/'
    response = requests.get(url = url)
    ht = response.text
    tree = etree.HTML(ht)
    print(tree.xpath("//img[@class= 'icon-image']/@alt |  //img[@class= 'icon-image']/@src"))
    

    ['/static/images/app_icon.png', '应用游戏', '/static/images/huati_icon.png', '话题', '/static/images/kutu_icon.png', '酷图', '/static/images/wode_icon.png', '我的', '/static/images/zhibo_icon.png', '应用吧', '/static/images/shoujiba_icon.png', '手机吧', '/static/images/kankanhao_icon.png', '看看号', '/static/images/shoucangjia_icon.png', '收藏单', '/static/images/yingyongji_icon.png', '应用集', '/static/images/shiyanshi_icon.png', '实验室', '/static/images/zidingyi_icon.png', '自定义设置', '/static/images/houtaichunjing_icon.png', '后台纯净', '/static/images/kaifazhe_icon.png', '开发者平台', '/static/images/shipinbofang_icon.png', '微发现', '/static/images/android_white.png', '安卓下载', '/static/images/iphone_white.png', 'iPhone下载', '/static/images/android_white.png', 'Android下载', '/static/images/iphone_white.png', 'iPhone下载']
    
  • 相关阅读:
    sed command
    【Python3】作用域(局部变量、全局变量)
    【Python3】函数与参数
    【Python3】编程范式
    【Python3】字符解码与编码
    【Python3】文件操作
    【Python3】集合
    【Python3】目录
    【Python3】字典
    【Python3】字符串操作
  • 原文地址:https://www.cnblogs.com/notfind/p/11470814.html
Copyright © 2011-2022 走看看