Vaccinations can prevent certain types of cancer by protecting against infections that can cause cancer. For example, the HPV vaccine protects against human papillomavirus, which can cause cervical, anal, and other cancers. Similarly, the hepatitis B vaccine can prevent liver cancer by protecting against the hepatitis B virus.