Can python crawler return a specified number of matching records (re modules)

  python

Re.findall () returns all matching records

import re
 
 P = re.compile(r'\d plus')
 print p.findall('one1two2three3four4')
 print p.findall('one1two2three3four4')[0:2]
 
 ### output ###
 # ['1', '2', '3', '4']
 # ['1', '2']

You can use slicing operationReturnTo deal withfindallReturn the results to achieve your goal

Or

import re
 
 count = 0
 # The end point number
 endpoint = 2
 
 P = re.compile(r'\d plus')
 for m in p.finditer('one1two2three3four4'):
 Count plus = 1
 if count > endpoint: break
 print m.group()
 
 ### output ###
 # 1 2