These days, Amazon do a lot of print-on-demand which gives bootleg quality, and there's no way to tell if you're going to get it, and there seems to be no way to avoid it.
Go with reputable publishers (e. g. O'Reilly or No Starch Press) and you will get quality books.
Ironic. Selling you quality books at a good price is how Amazon started.