• lxml中的etree(待补)


    待查看源码,为什么打印出来的类型是这样
    类型集合返回如下:

    1. <class 'list'>
      2.<class 'lxml.etree._Element'>
      3.<class 'lxml.etree._ElementUnicodeResult'> # 这里就是普通字符串了,可以执行写入文件操作
    import xlwt
    import requests
    from lxml import etree
    import time
    
    
    def getOnePage(url):
        html = requests.get(url)
        selector = etree.HTML(html.text)
        # 查询节点的话表格就要表格的节点,这里以ul节点查询为例//ul[@class="all-img-list cf"]/li
        # XPath定位节点返回的是节点集合
        infos = selector.xpath('//*[@id="book-img-text"]/ul/li') # //*[@id="book-img-text"]/ul/li[1]
        # 打印一些节点
        print(infos) 
        print(type(infos)) # <class 'list'>
        for info in infos:
            print(type(info)) # <class 'lxml.etree._Element'>
            print(info) # <Element li at 0x19dc2c7e0c8>
            print('
    ')
            style_1 = info.xpath('div[2]/p[1]/a[2]/text()')[0]
            style_2 = info.xpath('div[2]/p[1]/a[3]/text()')[0]
            print(style_1,type(style_1)) # <class 'lxml.etree._ElementUnicodeResult'>
            print('
    ')
            print(style_2,type(style_2))
    getOnePage('https://www.qidian.com/all/page%7B%7D/') # 打印出来的尽然是内存地址列表
    
    努力拼搏吧,不要害怕,不要去规划,不要迷茫。但你一定要在路上一直的走下去,尽管可能停滞不前,但也要走。
  • 相关阅读:
    网页HTML到8.20前
    数据库SQLServer
    构建之法读后感
    VS2013 生成安装文件
    工大助手(自动化部署)
    工大助手(用户名、密码错误提示)
    工大助手(验证码错误提示)
    工大助手(加权成绩计算)
    Wireshark插件编写
    微软认知服务——人脸识别
  • 原文地址:https://www.cnblogs.com/wkhzwmr/p/15311502.html
Copyright © 2020-2023  润新知