Sometimes it's a mistake, they actually think they're biting a fish or a seal.
That won't improve your mood if you're the person getting bitten but it might explain* why most shark attacks are non-fatal. They take a bite, don't like it and swim off.
Other attacks happen when humans stray into an area of the sea that the shark considers its own. The shark hits back to defend its territory.
Cases when a shark specifically targets a human are very rare, George Burgess, of the University of Florida has estimated this only happens about once a year
* Some experts say non-fatal attacks can also happen when the shark takes a bite, then swims away to allow you to bleed to death. At this point most humans are rescued by people nearby.