AttributeError: lxml.etree._Element object has no attribute xpth

import lxml.etree as le

# 分析 xpath 语句,提取1级分类
# xpath1=xpth('//div[@class="floor-container popular_recommend"]/text()')
# 继续分析 HTML结构,提取2级分类
# xpath2=xpath('//div[@class="floor-container popular_recommend"]/ul/li/a//div[@class="title"]/text()')

with open('edu.html','r',encoding='utf-8')as f:
    # 读取本地的edu.html
    html = f.read()
    # 把html 转成XML对象
    html_x = le.HTML(html)
    # 得到6个div对象
    div_x_s = html_x.xpth('//div[@class="classify_cList"]')
    print(div_x_s)
    #print(len(div_x_s))
    # 构造数据存储对象
    #data_s=[]
    # 对6个包含包含1级分类和2级分类对象做遍历
    #for div_x in div_x_s:
        # 得到一级分类
       # category1 = div_x.xpath

得到的返回结果报错,提示attributeError no attribute xpth

D:\Python3.8.5\python.exe D:\作业\模块3\第一章\myXpath_.py
Traceback (most recent call last):
  File "D:\作业\模块3\第一章\myXpath_.py", line 14, in <module>
    div_x_s = html_x.xpth('//div[@class="classify_cList"]')
AttributeError: 'lxml.etree._Element' object has no attribute 'xpth'

Process finished with exit code 1

经过逐条语句查询后,发现是xpth写错了,更改为xpath,重新运行OK

D:\Python3.8.5\python.exe D:\作业\模块3\第一章\myXpath_.py
[<Element div at 0x3335e48>, <Element div at 0x3335ea8>, <Element div at 0x3335ec8>, <Element div at 0x3335ee8>, <Element div at 0x3335f08>, <Element div at 0x3335f48>]
6

Process finished with exit code 0

代码本身其实并不复杂,关键是书写的正确性、严谨性,细节上。

GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:2 个月前 )
c345bb45 6 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐