"the world needs males and females, better that your children be male." How can a person avoid having female children?
It seems that the Gemara is not teaching practice , how one should conduct himself (e.g., to not become a tanner, to not give birth to females), but rather it is teaching knowledge ... that a person should not be distressed that he has no female children, but rather "happy is the person whose children are males"...
Kollel Iyun Hadaf
There is a way [to avoid having female children]. Shmuel says in Massechet Niddah how one can make all his children males.
The Gemara indeed gives advice about what to do in order to have children who are males (although not in the name of Shmuel) in Nidah 70b (at the end) and Eruvin 100b. Excellent suggestion!