正则表达式基本用法
# coding: utf-8
# python 内置re模块,用来支持正则表达式
import re
string = 'abccccccdedfdgbgds'
# 1.构造正则表达式
# a.* 贪婪模式 匹配到abbcdefggs全部
# a.*? 非贪婪模式 匹配到a 尽可能少的匹配字符
pattern = re.compile('a.*b')
# 2.使用正则表达式,在大字符串中搜索数据
# match() 1.正则表达式 2.要进行搜索的字符串
# match() 如果搜索的字符串是以正则表达式开头的字符,则可以搜索到结果,返回结果对象,如果搜不到,则返回None
rs = re.match(pattern, string)
# 判断结果是非为空,如果为空,就不再取值
if rs:
print rs.group()
else:
print '没有查询到结果!'
# search() 搜索函数 不需要以正则表达式开头,只要在大字符串中存在符合正则的字符,就可以查到,会搜索第一个匹配的数据
pattern1 = re.compile('b.*?d')
# search()函数的参数 1.正则 2.搜索字符串
rs = re.search(pattern1, string)
if rs:
print rs.group()
else:
print '没有匹配到数据'
# findall() 会将大字符串中所有符合规则字符全部找到,并且以列表的形式返回,如果搜索不到结果,返回一个空列表
# 1.正则 2.搜索字符串
rs = re.findall(pattern1, string)
if rs:
for content in rs:
print content
# r把后面的字符串原样保存 原样字符串,不会对字符串中的转译字符进行转译
string_2 = r'1.查找学员\n2.添加学员\n3.退出'
# replace() 替换源字符串中的某些字符
rs = string_2.replace(r'\n',',')
print rs
# sub() 替换原字符串中的某些符合条件的数据
pattern2 = re.compile(r'\\n')
# 1.正则 2.替换后的字符串 3.要进行替换的字符串
rs = re.sub(pattern2, ',', string_2)
print rs
string3 = 'abcdefghigkdlmhn'
pattern3 = re.compile(r'd.*?h')
rs = re.sub(pattern3,'------',string3)
print rs
将上一个用字符串匹配截取图片链接改写为用正则表达式来匹配图片链接
# coding: utf-8
import re
# 从给定链接中爬图片
string = '<cc><div id="post_content_115101375872" class="d_post_content j_d_post_content ">秋高气爽<br><img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=16a7318cd2f9d72a17641015e42b282a/353680cb39dbb6fd5e8db0950224ab18952b379e.jpg" size="65387" changedsize="true" width="560" height="420" size="65387"><br><img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=80df3901b9b7d0a27bc90495fbee760d/1ddd0b55b319ebc49aae232a8926cffc1c17169e.jpg" size="50323" changedsize="true" width="560" height="420" size="50323"><br><img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=078b542b55df8db1bc2e7c6c3922dddb/33c010dfa9ec8a1338cc0253fc03918fa2ecc09f.jpg" size="78770" changedsize="true" width="560" height="373" size="78770"><br><img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=f4602793b212c8fcb4f3f6c5cc0292b4/b0c62934349b033bdd5ea9691ece36d3d739bd9f.jpg" size="95035" changedsize="true" width="560" height="373" size="95035"><br><img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=1f8f63cc73f0f736d8fe4c093a54b382/300b37d12f2eb9382fbb2f41de628535e7dd6f9f.jpg" size="100285" changedsize="true" width="560" height="373" size="100285"><br><img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=cf96d68331f33a879e6d0012f65d1018/6b5f0e2442a7d933d23ab1c0a64bd11371f001da.jpg" size="65247" changedsize="true" width="560" height="420" size="65247"><br><img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=dfff2f41de62853592e0d229a0ee76f2/4a9f8ad4b31c8701e524e7552c7f9e2f0508ffdb.jpg" size="79750" changedsize="true" width="560" height="414" size="79750"><br><img class="BDE_Image" src="https://imgsa.baidu.com/forum/w%3D580/sign=b4a6d35d9782d158bb8259b9b00b19d5/35d1279759ee3d6d3054d27848166d224d4adedb.jpg" size="103175" changedsize="true" width="560" height="414" size="103175"></div><br></cc><a href="https://www.baidu.com"></a>'
# 1.构造正则表达式
pattern = re.compile(r'<img class="BDE_I.*?src="(.*?)".*?size="(.*?)".*?width="(.*?)".*?height="(.*?)"')
# 2.findall()查找所有符合规则的字符串
rs = re.findall(pattern, string)
for detail in rs :
print '图片链接:%s'%detail[0]
print '图片大小:%s'%detail[1]
print '图片宽度:%s'%detail[2]
print '图片高度:%s'%detail[3]
print '**********************************************'
例子:
利用正则表达式爬取糗事百科,将数据直接爬取到控制台
# coding: utf-8
import requests
import re
# 1.准备url
url = 'https://www.qiushibaike.com/hot/'
# 2.发起请求,拿回源代码
response = requests.get(url)
# 取出html源代码
html = response.content
# ps:需要从html中根据正则匹配到总页数,转换为整数
# re.S DOTALL模式 .可以用来匹配所有的任意字符
# 该句是从冒号之后进行匹配的
pattern1 = re.compile(r'class="dots.*?<span.*?>(.*?)</span>',re.S)
rs = re.search(pattern1,html)
# group(index)根据分组索引查找内容
# strip函数()去除字符串中的某些字符
total_page = int(rs.group(1).strip('\n'))
print total_page
print '共%s页段子内容!'%total_page
# for 循环获取每一页的html源代码
for x in range(1,total_page+1):
print '正在爬去第%s页段子...'%x
url = 'https://www.qiushibaike.com/hot/page/%s/'%x
html = requests.get(url).content
# 根据正则匹配每一页的段子内容
# 3.准备正则
# re.S DOTALL模式 .可以用来匹配所有的任意字符
pattern = re.compile(r'<div class=".*?qiushi_tag.*?<h2>(.*?)'
r'</h2>.*?<div class="articleGender.*?>(.*?)'
r'</div>.*?<span>(.*?)</span>.*?<i class="n.*?>(.*?)'
r'</i>.*?<i.*?>(.*?)</i>', re.S)
# 4.查找所有的符合规则的数据
rs = re.findall(pattern,html)
for detail in rs:
# 取出数据
name = detail[0]
# strip函数()去除字符串中的某些字符
name = name.strip('\n')
age = detail[1]
content = detail[2]
content = content.strip('\n')
# 准备正则
pattern1 = re.compile(r'<br/>')
# 使用sub函数做替换
content = re.sub(pattern1 ,'\n', content)
vote_number = detail[3]
comment_number = detail[4]
print '---------------------------------------------------------'
print '用户名:%s 年龄:%s'%(name,age)
print '好笑数:%s 评论数:%s'%(vote_number, comment_number)
print content