Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python beautifulsoup get html tag content

How can I get the content of an html tag with beautifulsoup? for example the content of <title> tag?

I tried:

from bs4 import BeautifulSoup

url ='http://www.websiteaddress.com'
soup = BeautifulSoup(url)
result = soup.findAll('title')
for each in result:
    print(each.get_text())

But nothing happened. I'm using python3.

like image 653
niloofar Avatar asked Dec 02 '25 09:12

niloofar


1 Answers

You need to fetch the website data first. You can do this with the urllib.request module. Note that HTML documents only have one title so there is no need to use find_all() and a loop.

from urllib.request import urlopen
from bs4 import BeautifulSoup

url ='http://www.websiteaddress.com'
data = urlopen(url)
soup = BeautifulSoup(data, 'html.parser')
result = soup.find('title')
print(result.get_text())
like image 195
pp_ Avatar answered Dec 04 '25 01:12

pp_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!