Home > Blockchain >  Not able to fetch titles of a website using BS4
Not able to fetch titles of a website using BS4

Time:08-24

import requests
from bs4 import BeautifulSoup

url = "https://youtube.com/"
r = requests.get(url)
htmlContent = r.content

soup=BeautifulSoup(htmlContent, 'html.parser')

title = soup.title()
print(title)

Been getting an empty list as an output for this.

CodePudding user response:

Try this

import requests
from bs4 import BeautifulSoup

url = "https://youtube.com/"
r = requests.get(url)
htmlContent = r.content

soup=BeautifulSoup(htmlContent, 'html.parser')

title = soup.find("title").text
print(title)

or

title = soup.title.text

CodePudding user response:

Seems to be a typo - simply remove the () to get the <title>:

soup.title
### <title>YouTube</title>

Add .text to get the text of the <title>:

soup.title.text
### YouTube
  • Related