Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在HTML中匹配P标记中的文本_Python_Html_Regex - Fatal编程技术网

Python 在HTML中匹配P标记中的文本

Python 在HTML中匹配P标记中的文本,python,html,regex,Python,Html,Regex,我想使用python正则表达式匹配html中每个段落的内容。这些段落中始终有BR标记,如下所示: <p class="thisClass">this is nice <br /><br /> isn't it?</p> 查找所有匹配项。然而,它只匹配了我28段中的两段,看起来这是因为这两段没有BR标签,其余的都有。我做错了什么?我能做些什么来修复它?谢谢 我不认为它失败是因为,而是因为该段落跨越多行。使用模式修复此问题: pattern = re

我想使用python正则表达式匹配html中每个段落的内容。这些段落中始终有BR标记,如下所示:

<p class="thisClass">this is nice <br /><br /> isn't it?</p>

查找所有匹配项。然而,它只匹配了我28段中的两段,看起来这是因为这两段没有BR标签,其余的都有。我做错了什么?我能做些什么来修复它?谢谢

我不认为它失败是因为

,而是因为该段落跨越多行。使用模式修复此问题:

pattern = re.compile('<p class=\"thisClass\">(.*?)<\/p>', re.DOTALL)
pattern=re.compile('

(.*),re.DOTALL)


原来答案是将re.S作为一个标志,允许“.”字符也匹配换行符

pattern = re.compile('<p class=\"thisClass\">(.*?)<\/p>', re.S)
pattern=re.compile('

(.*),re.S)


这非常有效。

或者更确切地说,您应该使用re.DOTALL模式使点也与换行符匹配。谢谢你的回答!我知道这是一个新手的错误;)这是通往DOTALL模式的快捷方式
pattern = re.compile('<p class=\"thisClass\">(.*?)<\/p>', re.DOTALL)
pattern = re.compile('<p class=\"thisClass\">(.*?)<\/p>', re.S)