The "Buy" operation doesn't target a specific card. It targets a specific card name in the supply. In other words, what you do when you buy a card isn't this:
(1a) Choose a card in the supply
(1b) Pay for it
(1c) Gain the chosen card
It's this:
(2a) Choose a card name of which there is a copy in the supply
(2b) Pay for it
(2c) Gain a copy of the chosen card
We can tell by means of Talisman, whose relevant effect is:
"When you buy a card... gain a copy of it."
As we know, things that trigger on-buy take place before the gain that is the result of the buy. So if you buy a Pearl Diver with Talisman in play, you gain a copy of Pearl Diver before you gain the one that you're actually buying. Naturally you gain the Pearl Diver on top of the pile.
So: if the Buy operation targets a specific card—again necessarily the top one of the pile—then in the case of Talisman, by the time you get around to gaining the bought card, you've already gained it, through Talisman's on-buy effect, and there's nothing left to gain through the actual Buy operation. If the Buy operation targets a card name, then it doesn't care that the Pearl Diver you were trying to buy is already gone from the supply by the time you get around to gaining it; there's no specific Pearl Diver you were trying to buy, and any card named "Pearl Diver" will do.