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('\n')
        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('\n')
        print(style_2,type(style_2))
getOnePage('https://www.qidian.com/all/page%7B%7D/') # 打印出来的尽然是内存地址列表
posted @ 2021-09-19 14:25  索匣  阅读(336)  评论(0编辑  收藏  举报