模块三 第一周 作业一 xpath应用

1 问题解析

在这里插入图片描述

2 解题提示

  1. 谷歌xpath的添加
  2. xml的理解
  3. xpath的使用

3 评分标准

  1. 写出正确的XPath语句10分
  2. 成功输出课程分类 10分
  3. 代码注释,规范10分

4 要点解析

谷歌添加xpath方法

加载本地xpath插件步骤说明

xpath

5 代码实现

import lxml.etree as le
import urllib.request as ur


# 请求url
# request=ur.Request('https://edu.csdn.net')
# # 读取页面
# response=ur.urlopen(request).read()
# # 保存页面
# with open('edu1.html','wb')as f:
#     #写入html文件字符串格式
#     html=f.write(response)
# 读取页面,进行解析
with open('edu1.html', 'r', encoding='utf-8')as f:
    # 读取html文件字符串格式
    html1 = f.read()
    # html文件转换成xpath格式,可以对这个对象进行索引
    html_x = le.HTML(html1)
    # 对一级标题进行索引
    div_x_s = html_x.xpath('//div[@class="classify_cList"]')
    data_s = []
    for div_x in div_x_s:
        # 一级标题
        category1 = div_x.xpath('./h3/a/text()')
        # 二级标题
        category2_s = div_x.xpath('./div/span/a/text()')
        # 将一级和二级标题加入空列表内
        data_s.append(
            {
                'category1': category1,
                'category2_s': category2_s
            }
        )
        # 通过遍历data将一级标题与二级标题输出
        print(data_s)
    for data in data_s:
        # 输出一级标题
        print(data.get('category1'))
        print('   ', data.get('category2_s'))
发布了161 篇原创文章 · 获赞 37 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/ybw_2569/article/details/104599733