Selenium (Se) is a prospective candidate of cathode material for high-energy batteries. However, the dissolution of high-order lithium selenides and the shuttle effect in electrolytes lead to low Se utilization, inferior capacity and poor cycling performance. Herein, a heteroatoms (N and O) dual-doped hierarchical porous carbon (HDHPC) with interconnected macro/meso/micropores is used to confine Se composite and developed as a cathode material for advanced Li/Na–Se batteries in low-cost carbonate electrolyte. For Li-Se batteries, Se/HDHPC cathode delivers an initial charge capacity of 613 mAh g−1 and maintains stable reversible capacity of 545 mAh g−1 with a capacity decay as low as 0.0074% per cycle after 1500 cycles at a current density of 0.5 C (1 C = 675 mA g−1). For Na-Se batteries, Se/HDHPC exhibits a high initial charge capacity of 619 mAh g−1 and retains as high as 402 mAh g−1 after 500 cycles at a current rate of 0.5 C. Moreover, Se/HDHPC composite also exhibits an excellent rate performance both in the Li-Se and Na-Se batteries even at a high rate up to 20 C (13.5 A g−1). Such excellent electrochemical properties of Se/HDHPC are mainly contributed from two aspects. One is the hierarchical porous structure of the HDHPC, which benefit the penetration of electrolyte and also act as micro reaction chambers to effectively suppress the volume change of the electrode. The other is the strong interaction between lithium/sodium selenide and carbon host induced by heteroatoms (N and O) dual-doping, which can strongly anchor the Se-contained species, thus preventing the side reaction of Se anions with electrolyte.