正则:
例子
这是一个html文件,在pycharm中用正则的方法提取里面的分类和课程
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSDN微课商城</title>
<link rel="stylesheet" href="../css/main.css">
<script type="text/javascript" src="../js/main.js"></script>
</head>
<body>
<div id="register" hidden="hidden">
<h2 class="form_p">注册</h2>
<p id="register_message">
<!--信息有误-->
</p>
<form action="#" method="post" id="register_form">
<input id="register_account" type="text" name="account" placeholder="账号(数字、英文、下换线,8-16位)"><br/>
<input id="register_password" type="password" name="password" placeholder="密码(数字、英文、下换线,6-16位)"><br/>
<!--<input type="password" name="repassword" placeholder="确认密码"><br/>-->
<input id="register_submit" type="submit" value="注册">
</form>
</div>
<div id="login" hidden="hidden">
<h2 class="form_p">登录</h2>
<p id="login_message">
<!--信息有误-->
</p>
<form action="#" method="post" id="login_form">
<input id="login_account" type="text" name="account" placeholder="账号"><br>
<input id="login_password" type="password" name="password" placeholder="密码"><br>
<input id="login_submit" type="submit" value="登录">
</form>
</div>
<header>
<span class="title"> <a href="index.html">CSDN微课商城</a> </span>
<span>
<form action="#" class="search_form">
<input type="text" name="course" placeholder="按课程名称搜索">
<input type="submit" value="搜索">
</form>
</span>
<span class="user">
<a href="javascript:show('login')">登录</a>/
<a href="javascript:show('register')">注册</a>
<!-- 已经登录显示的内容 -->
你好:
<a href="user.html">用户1</a>
<a href="#">注销</a>
</span>
</header>
<article>
<section class="nav_section"><img src="../img/csdn_static/2.png" alt="" width="100%"></section>
<section class="main_section"><h1>第一章 路由与模板</h1>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">Web原理与框架简介</span><span class="price">¥75</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">Django环境搭建与入门案例</span><span class="price">¥153</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">基本路由映射与命名空间</span><span class="price">¥154</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">正则路由映射参数的传递与接收</span><span class="price">¥177</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">反向解析处理器</span><span class="price">¥161</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">Request对象与Response对象</span><span class="price">¥44</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">上下文与模板调用</span><span class="price">¥97</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">模板层基础语法</span><span class="price">¥105</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">模板过滤器</span><span class="price">¥133</span></figcaption>
</a></figure>
</section>
<section class="main_section"><h1>第二章 模型类实现</h1>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">ORM原理与数据库配置</span><span class="price">¥143</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">表与字段的定义和常用字段约束</span><span class="price">¥118</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">数据迁移与维护</span><span class="price">¥57</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">模型类的增删改</span><span class="price">¥45</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">模型类的查询方法</span><span class="price">¥187</span></figcaption>
</a></figure>
<figure><a href="course.html"> <img src="../img/course/course.png">
<figcaption><span class="course_name">QuerySet详解</span><span class="price">¥197</span></figcaption>
</a></figure>
</section>
</article>
<footer>
<div id="footer_div1">
<p><a href="#">关于我们</a>| <a href="#">招聘</a>| <a href="#">广告服务</a>| <a href="#">网站地图</a></p>
<p><a href="#">QQ客服</a>| <a href="#">kefu@csdn.ent</a>| <a href="#">客服论坛</a>| <a href="#">400-660-0108</a>| <a
href="#">工作时间:8:30-22:00</a></p>
<p> 百度提供站内搜索 北ICP备19004658 </p>
<p> ©1999-2019 北京创新乐知网络技术有限公司 </p>
<p> 版权申诉 家长监护 经营性网站备案信息 网络110报警服务 中国互联网举报中心 北京互联网违法和不良信息举报中心 </p>
</div>
<div id="footer_div2">
<figure><img src="../img/csdn_static/二维码1.png">
<figcaption>CSDN咨询</figcaption>
</figure>
<figure><img src="../img/csdn_static/二维码1.png">
<figcaption>CSDN学院</figcaption>
</figure>
<figure><img src="../img/csdn_static/二维码1.png">
<figcaption>CSDN企业招聘</figcaption>
</figure>
</div>
</footer>
</body>
</html>
正则语法:虽然强大,但是有点复杂。
import re
with open('./heml/index.html','r',encoding='utf-8') as f:
# html = f.read()
html = re.sub('\n','',f.read()) # 去掉换行符
# print(html)
section_pattern = '<section class="main_section">(.*?)</section>'
section_s = re.findall(section_pattern,html) # 查找session的种类
# print(section_s)
# print(len(section_s))
category_pattern = '<h1>(.*?)</h1>'
course_pattern = '<span class="course_name">(.*?)</span>' # 课程的正则表达式
data_s = []
for section in section_s:
category = re.findall(category_pattern,section)[0]
# print(category) # 打印其session的名称
course_s = re.findall(course_pattern,section)
data_s.append(
{
'category': category,
'course_s': course_s
}
)
print(data_s)
for data in data_s:
print(data.get('category'))
for course in data.get('course_s'):
print(' ',course)
x-path
将x-path安装在谷歌浏览器中。它的语法有:
/ 根节点选取或下级
// 任意节点,不考虑位置
. 当前节点
… 当前节点的父节点
@ 选取属性
-
匹配任何节点
【nodename】 根据节点筛选
contains(@属性,“包含的内容”) 模糊查询
text() 文本内容
x-path的索引从1开始!
例子
在网站中点击检查,可以看见属性值:
在x-path中输入://h3[@class="classify_c_h3"]/a/text()
即可找到所选取的一级分类内容文本。
二级分类的文本内容:
输入的表达式://div[@class="classify_list"]/span/a/text()
在真正的使用中,我们一般会采用模糊查询:
//div[contains(@class,"classify_list")/span/a/text()