我需要从HTML源文件中找到表单的内容,我做了一些搜索并找到了很好的方法来做到这一点,但问题是它只打印出第一个找到的,我怎么能循环它并输出所有的表单内容,而不是只是第一个?
line = 'bla bla bla<form>Form 1</form> some text...<form>Form 2</form> more text?'
matchObj = re.search('<form>(.*?)</form>', line, re.S)
print matchObj.group(1)
# Output: Form 1
# I need it to output every form content he found, not just first one...
Run Code Online (Sandbox Code Playgroud)
Pet*_*rin 55
但是,如果您需要在字符串中找到所有正则表达式匹配项,请使用该findall函数.
import re
line = 'bla bla bla<form>Form 1</form> some text...<form>Form 2</form> more text?'
matches = re.findall('<form>(.*?)</form>', line, re.DOTALL)
print(matches)
# Output: ['Form 1', 'Form 2']
Run Code Online (Sandbox Code Playgroud)
Aam*_*nan 21
而不是re.search使用re.findall它将返回你的所有匹配List.或者您也可以使用re.finditer(我最喜欢使用它)它会返回一个Iterator Object,您可以使用它来迭代所有找到的匹配.
line = 'bla bla bla<form>Form 1</form> some text...<form>Form 2</form> more text?'
for match in re.finditer('<form>(.*?)</form>', line, re.S):
print match.group(1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
91632 次 |
| 最近记录: |