Abstract In this work, a rapid and selective method was successfully developed using the magnetic molecularly imprinted polymer (MMIP) as sorbent for the extraction of β-lactam antibiotics (BLAs) from milk samples. The MMIP has been prepared using penicillin V potassium (PENV) as template molecule, methacrylic acid as functional monomer, ethylene glycol dimethacrylate as crosslinking agent and Fe 3O 4 magnetite as magnetic component. The experimental results showed that the MMIP had high affinity and selectivity toward PENV and other structurally related BLAs. The extraction process was carried out in a single step by mixing the extraction solvent, MMIPs and milk samples under ultrasonic action. When the extraction was completed, the MMIPs adsorbing the analytes were separated from the sample matrix by an external magnet. The analytes eluted from the MMIP were analyzed by liquid chromatography–tandem mass spectrometry. For achieving optimal preconcentration and reducing non-specific interactions, various parameters affecting the extraction efficiency such as extraction mode, extraction solvent, the amount of MMIPs, extraction time, washing solution and eluting solution were comprehensively evaluated. Under the optimal conditions, the detection limits of BLAs are in the range of 1.6–2.8 ng mL −1. The relative standard deviations of intra- and inter-day ranging from 3.2% to 8.3% and from 3.6% to 9.8% are obtained, respectively. The method was applied to determine BLAs including PENV, amoxicillin and oxacillin in five milk samples from different provenances. The recoveries of BLAs in these samples from 71.6% to 90.7% are obtained.