150讲轻松搞定Python网络爬虫-第三章:数据解析

直接学习:https://edu.csdn.net/course/play/24756/280712
正则表达式-re模块常用函数

# 2、findall: 查找所有满足条件的
text="apple's price is $99, orange's price is $88"
result = re.findall('\$\d+',text)
print(result)

# 3、sub:根据规则替换其他字符串
text ='nihao, zhongguo, hello, world' 
result = text.replace(' ','\n')
result = re.sub(r' ','\n',text)
result = re.sub(r' |,','\n',text)
print(result)

html = '''
<p><span>1、一本以上学历,计算机网络及软件工程相关专业; </span><span></span></p>
<p><span>2、JAVA基础扎实,理解io、多线程、集合等基础框架,对JVM原理有一定的了解; </span><span></span></p>
<p><span>3、熟悉 Python及常用的 Web 开发框架(django); </span><span></span></p>
<p><span>4、对大数据感兴趣,对云计算有一定的认知,对移动端设备有一定了解; </span><span></span></p>
<p><span>5、学习能力强,拥有优秀的逻辑思维能力与自我管理能力以及团队合作能力。</span><span></span></p>
'''
new_html = re.sub(r'<.+?>',"",html)
print(new_html)

# 4、split:根据规则分割字符串
text ='nihao,zhongguo hello,world' 
result = re.split(r' |,',text)
print(result)

# 5、compile:编译正则表达式
text = "apple's price is 34.56"
r = re.compile(r'''
\d+   #代表整数
\.?   #小数点
\d*   #小数
''',re.VERBOSE)
result = re.search(r,text)
print(result.group())


如果想要在正则表达式中加注释,那么需要在正则表达式的函数后面加上’re.VERBOSE’

发布了67 篇原创文章 · 获赞 1 · 访问量 768

猜你喜欢

转载自blog.csdn.net/weixin_43597208/article/details/105300995