首先我们看下官方文档里关于的compile的说明:

re.compile(pattern, flags=0)
Compile a regular expression pattern into a regular expression object, which can be used for matching using its match() and search() methods, described below.The expression’s behaviour can be modified by specifying a flags value. Values can be any of the following variables, combined using bitwise OR (the | operator).
</pre><pre name="code" class="python">The sequence:
prog = re.compile(pattern)
result = prog.match(string)
<strong><span style="font-size:24px;">is equivalent to</span></strong>
result = re.match(pattern, string)
but using re.compile() and saving the resulting regular expression object for reuse is more efficient when the expression will be used several times in a single program.Note:The compiled versions of the most recent patterns passed to re.compile() and the module-level matching functions are cached, so programs that use only a few regular expressions at a time needn’t worry about compiling regular expressions.

下面是flag dotall的说明:

re.DOTALL
Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.

》》》》》》》》》》》》》》》》》》》》

下面是关于findall的说明:

re.findall(pattern, string, flags=0)
Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

》》》》》》》》》》》》》》》》》》》》

下面举个栗子进行讲解:

>>> import re
>>> s = "adfad asdfasdf asdfas asdfawef asd adsfas ">>> reObj1 = re.compile('((\w+)\s+\w+)')
>>> reObj1.findall(s)
[('adfad asdfasdf', 'adfad'), ('asdfas asdfawef', 'asdfas'), ('asd adsfas', 'asd')]>>> reObj2 = re.compile('(\w+)\s+\w+')
>>> reObj2.findall(s)
['adfad', 'asdfas', 'asd']>>> reObj3 = re.compile('\w+\s+\w+')
>>> reObj3.findall(s)
['adfad asdfasdf', 'asdfas asdfawef', 'asd adsfas']

代码参考下图进行理解:

对于上面的代码,我们可以看到:

findall函数返回的总是正则表达式在字符串中所有匹配结果的列表list,此处主要讨论列表中“结果”的展现方式,即findall中返回列表中每个元素包含的信息。

1.当给出的正则表达式中带有多个括号时,列表的元素为多个字符串组成的tuple,tuple中字符串个数与括号对数相同,字符串内容与每个括号内的正则表达式相对应,并且排放顺序是按括号出现的顺序。

2.当给出的正则表达式中带有一个括号时,列表的元素为字符串,此字符串的内容与括号中的正则表达式相对应(不是整个正则表达式的匹配内容)。

3.当给出的正则表达式中不带括号时,列表的元素为字符串,此字符串为整个正则表达式匹配的内容。

《《《《《《《《《《《《《《《《《

对于.re.compile.findall(data)之后的数据,我们可以通过list的offset索引或者str.join()函数来使之变成str字符串,从而进行方便的处理,下面是python3.5中str.join()的文档:

str.join(iterable)
Return a string which is the concatenation of the strings in the iterable iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects.The separator between elements is the string providing this method.

经过上面的介绍,相信对crawler里的正则有很大的帮助

Python 正则re模块之compile()和findall()详解相关推荐

  1. 【Python入门】Python之OS模块39个常用函数详解

    os,语义操作系统,所以该模块就是操作系统相关的功能了,用于处理文件和目录这些我们日常手动需要做的操作,比如新建文件夹.获取文件列表.删除某个文件.获取文件大小.重命名文件.获取文件修改时间等,该模块 ...

  2. python自带模块中文解释_python内置模块详解

    python内置模块详解 来源:中文源码网    浏览: 次    日期:2019年11月5日 [下载文档:  python内置模块详解.txt ] (友情提示:右键点上行txt文档名->目标另 ...

  3. python的pytest模块:pytest命令行详解

    一.官方文档 How to invoke pytest - pytest documentationhttps://docs.pytest.org/en/latest/how-to/usage.htm ...

  4. 【Python入门】Python之shutil模块11个常用函数详解

    shutil 是 Python 中的高级文件操作模块,与os模块形成互补的关系,os主要提供了文件或文件夹的新建.删除.查看等方法,还提供了对文件以及目录的路径操作.shutil模块提供了移动.复制. ...

  5. python的shutil模块是内置的_Python之shutil模块11个常用函数详解,python内置函数是什么...

    Python之shutil模块11个常用函数详解,python内置函数是什么 shutil 是 Python 中的高级文件操作模块,与os模块形成互补的关系,os主要提供了文件或文件夹的新建.删除.查 ...

  6. Python 之正则表达re.compile()与re.findall()详解

    在使用爬虫提取网页中的部分信息时,采用到了re.compile()与re.findall()两种方法,目的:把网页中的"某某城市土地规划表"截取并打印出来. 网页中的代码: < ...

  7. python调用包的路径_Python3 模块、包调用路径详解

    如下所示: ''' 以下代码均为讲解,不能实际操作 ''' ''' 博客园 Infi_chu ''' ''' 模块的优点: 1.高可维护性 2.可以大大减少编写的代码量 模块一共有三种: 1.Pyth ...

  8. python中paste函数的作用_PIL图像处理模块paste方法简单使用详解

    python2中提供了PIL基础的图像数据出来模块,在python3中更名为了pillow模块,名字虽然发生了改变,但是提供的方法和功能都是一样的,对于日常基础的图像数据处理分析来说是足够用了的,现在 ...

  9. python six模块详解_对python中的six.moves模块的下载函数urlretrieve详解

    实验环境:windows 7,anaconda 3(python 3.5),tensorflow(gpu/cpu) 函数介绍:所用函数为six.moves下的urllib中的函数,调用如下urllib ...

  10. python中append函数解析_对python中的pop函数和append函数详解

    对python中的pop函数和append函数详解 pop()函数 1.描述 pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值. 语法 pop()方法语法: list. ...

最新文章

  1. python详细安装教程3.7.4-python 3.7.4下载与安装的问题
  2. 【精品资源】干货分享:20款精美的手机网站模板下载
  3. Python自动化办公之Excel对比工具
  4. CI框架源码阅读笔记8 控制器Controller.php
  5. 【学习笔记11】动态方法调用和使用通配符定义action
  6. 文献记录(part1)--NP-hardness of Euclidean sum-of-squares clustering
  7. nohup命令输出日志_逼格高又实用的Linux高级命令,开发运维都要懂
  8. hanlp加载预训练模型
  9. Spark RDD 复杂算子
  10. 查看linux服务器内存使用情况,不够时创建Swap、手动 cached
  11. 泵车砼活塞故障预警-冠军方案
  12. 矩阵分析 (四)向量和矩阵的范数
  13. MySqlNav(可视化工具)的安装
  14. 计算机网络之了解计算机网络
  15. 好玩的小霸王游戏机HTML网站源码
  16. vs2003远程调试方法
  17. word批量调整图片大小:
  18. map获取key的方式
  19. 聊城大学计算机专业在全国排名,聊城大学排名
  20. 独家丨Web3风向从NFTs吹向DAOs?一季度DAO发展回顾

热门文章

  1. 《电动自行车充电领域的液体冷却技术研究》论文笔记
  2. python怎么读音发音英语-django的英文读法是什么
  3. 一行代码实现F11的功能,即让浏览器窗口全屏
  4. 计算机网络局域网之无线局域网
  5. 妄想性仮想人格障害 汉化补丁(BUG修正)
  6. ME909 ECM拨号上网总结
  7. 10、(十)外汇交易中专有名词整理
  8. 爬取《全职高手之巅峰荣耀》的豆瓣影评,分析漫改电影的优劣好坏
  9. 中文简繁转换项目 OpenCC
  10. 【Uipath杂谈】用Datatable处理数据(二)