- 导包 from lxml import etree
- 生成etree对象
- tree=etree.parse(fp) fp=open(xxx ,'r')将本地存储好的文件中数据加载到etree中进行解析
- tree=etree.HTML(html) 用来解析网络请求到的页面源码数据
- li_list=tree.xpath('//div/ul/li')or for li_list:标签名li.xpath('./a/@src') 局部解析
//和./的区别:
// 在最左侧的//:可以从任意位置进行标签的相对位置定位
不在最左侧的//,表示多个层级
/表示一个层级
./表示在当前层级下,表示xpath调用者对应的标签