Python 之Re模块

re简介

re模块是python独有的匹配字符串的模块,该模块中提供的很多功能是基于正则表达式实现的,而正则表达式是对字符串进行模糊匹配,提取自己需要的字符串部分,他对所有的语言都通用。

import re
# 方式一:
source_str='python1class'
# value=re.match('thon\dc',source_str).group()  #从开头开始匹配
value=re.search('thon\dc',source_str).group() #非开头匹配
print(value) #打印结果:thon1c

#方式二:
regexp=re.compile('thon\dc')
v=regexp.search(source_str).group()
print(v) #打印结果:thon1c

 re模块函数

1、findall(pattern,string[,flags=0])

匹配到字符串中所有符合条件的元素,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;空的匹配也会包含在结果中.

2、re.compile(pattern[,flags])

python代码最终会被编译为字节码,之后才被解释器执行,在模式匹配之前,正在表达式模式必须先被编译成regex对象,预先编译可以提高性能,re.compile()就是用于提供此功能。

import re
str='hello lv ll loo'
regexp=re.compile('l+') #匹配规则,在字符串str中查找t开始的一个或多个l
result=regexp.findall(str)#查询符合匹配规则的字符串
print(result)  #打印结果:['ll', 'l', 'll', 'l']

实战一:re实战截取响应部分

import re
import requests
#实战一:re实战截取响应部分值
response=requests.get('https://www.baidu.com')
response.encoding='utf-8'
body_str=response.text

title_value=re.findall('<title>(.+?)</title>',body_str)[0]
print(title_value) #结果:百度一下,你就知道

 

实战二:接口串联实例

#登录微信公众号开发平台,修改已存在的标签
#第一步:获取access_token
get_param_data={
    "grant_type":"client_credential",
    "appid":"wx93fc8716b3795e",
    "secret":"1ff879affa4d6c7cddc27b2e994069"
}
response=requests.get('https://api.weixin.qq.com/cgi-bin/token',get_param_data)
body_str=response.content.decode('utf-8')
#通过正则表达式制定匹配token的规则,并从返回的正文中查找符合规则的字符串
token_value=re.findall('"access_token":"(.+?)"',body_str)[0]
token_value=response.json()['access_token']
print(token_value)

#第二步:把获取的access_token值传给修改标签接口
get_param_data={
'access_token':token_value  #把之前获取的token值传给access_token
}

post_param_data={"tag": {"id":103,"name":"上海666"}}
header_info={'Content-Type':'application/json'}  #发送json数据必带的头部信息

response=requests.post(url='https://api.weixin.qq.com/cgi-bin/tags/update',
                       params=get_param_data,
                       # data=json.dumps(post_param_data),
                       json=post_param_data,
                       headers=header_info
                       )
print(response.content.decode('utf-8'))  #结果:{"errcode":0,"errmsg":"ok"}

 

 

 

posted @ 2021-06-27 21:58  重走青春LV  阅读(272)  评论(0编辑  收藏  举报