27
Scrape news headlines with python in <10 lines of code!
First of all, make sure to import these libraries at the beginning of your python script:
import requests
from bs4 import BeautifulSoup
For this tutorial, I'll be using BBC news as my news source, use these 2 lines of code to get it's url:
url='https://www.bbc.com/news'
response = requests.get(url)
Now we're ready to scrape using BeautifulSoup!
Head over to BBC news and inspect a news headline by right clicking and pressing inspect.
As you'll see, all news headlines are contained within an "h3" tag:
As you'll see, all news headlines are contained within an "h3" tag:

Now add these 4 lines of code to scrape and display all the h3 tags from BBC news:
soup = BeautifulSoup(response.text, 'html.parser')
headlines = soup.find('body').find_all('h3')
for x in headlines:
print(x.text.strip())
Full code
import requests
from bs4 import BeautifulSoup
url='https://www.bbc.com/news'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
headlines = soup.find('body').find_all('h3')
for x in headlines:
print(x.text.strip())
Byeeeeeđź‘‹
27