A compound of formula (I) or (II) and use of the compound in the preparation of drugs for treating cancer are disclosed. The study shows that the compounds can inhibit the growth of many kinds of tumor cells, can be used for targeting epidermal growth factor receptor (EGFR), and particularly can inhibit tumor cells with single or multiple mutations of EGFR (T790M). Therefore, the compound can be used as EGFR inhibitor to treat cancer and has a relatively large application value.