Objective: Screening out potential herbal medicines and herbal ingredients for the treatment of gastric cancer based on transcriptomic analysis of immune infiltration and ferroptosis.Methods: Gene expression profiles of gastric tumour tissues and normal tissue samples were obtained from the GEO database and the samples were analysed for immune cell infiltration condition and differential expressed genes of ferroptosis. Key genes were screened by protein-protein interaction (PPI) and enrichment analysis, and molecular docking was used to predict and preliminary validate potential herbal and traditional Chinese medicine components for gastric cancer based on the key genes. Finally, RT-QPCR was used to validate the prediction results.Results: Immune cell infiltration analysis revealed high levels of infiltration of activated CD4 memory T cells, monocytes, M0 macrophages in gastric tumor tissues, while plasma cells and resting mast cells had higher levels of infiltration in the paraneoplastic tissues. Differential gene expression analysis identified 1,012 upregulated genes and 880 downregulated genes, of which 84 immune related differentially expressed genes such as CTSB, PGF and PLAU and 10 ferroptosis-related differentially expressed genes such as HSF1, NOX4 and NF2 were highly expressed in gastric cancer tissues. The results of enrichment analysis showed that they mainly involve 343 biological processes such as extracellular matrix organization and extracellular structural organization; 37 cellular components such as complexes of collagen trimer and basement membrane; 35 molecular functions such as signal receptor activator activity and receptor ligand activity; 19 regulatory pathways such as cytokine-cytokine receptor interactions and retinol metabolism. Finally, two key genes, TLR4 and KRAS, were selected and 12 herbal medicines such as Radix Salviae liguliobae, Rhizoma Coptidis, Rhizoma Polygoni cuspidati and 27 herbal ingredients such as resveratrol, salvianolic acid b were predicted on the basis of key genes. Molecular docking results showed that KRAS binds tightly to coumarin and magnolol, while TLR4 can bind tightly to resveratrol, curcumin, salvianolic acid b, shikonin. Subsequently, the effect of resveratrol and magnolol was experimentally verified.Conclusion: Herbal medicines such as S. liguliobae, Rhizoma Coptidis, Rhizoma P. cuspidati and herbal ingredients such as resveratrol, curcumin, salvianolic acid b may provide research directions and alternative therapeutic approaches for immunomodulation of TME and ferroptosis of tumour cells in gastric cancer.