150 speak easily get Python web crawler - Chapter 3: data analysis

Direct study: https: //edu.csdn.net/course/play/24756/280712
regular expressions -re module commonly used functions

# 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())


If you want to add a comment expressions in the positive, then the latter function requires regular expression plus 're.VERBOSE'

Published 67 original articles · won praise 1 · views 768

Guess you like

Origin blog.csdn.net/weixin_43597208/article/details/105300995