Tapeworms are gotten in one of two ways...

Directly injesting a flea (the flea is an intermediate host)

OR

Eating rodents (which carry fleas)

Tapeworms can take about a month to show up in the stool or on the rear, so if you're seeing them now, then it was from about a month ago.

Tapeworms are extremely difficult to diagnose in a fecal exam, it is usually diagnosed when actual tapeworm segments are seen on the pet.

Cornell University

Centers For Disease Control (CDC) Tapeworm

Veterinary Partner-tapeworms