  In this paper, for an odd prime $p$, by extending Li et al.'s construction
\cite{CL2016}, several classes of two-weight and three-weight linear codes over
the finite field $\mathbb{F}_p$ are constructed from a defining set, and then
their complete weight enumerators are determined by using Weil sums.
Furthermore, we show that some examples of these codes are optimal or almost
optimal with respect to the Griesmer bound. Our results generalize the
corresponding results in \cite{CL2016, GJ2019}.
