2016-02-28 106 views
0

如何獲取html標記與美麗的內容?例如<title>標籤的內容?python beautifulsoup獲取html標記內容

我想:

from bs4 import BeautifulSoup 

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

但是什麼都沒有發生。我正在使用python3。

回答

2

您需要先獲取網站數據。您可以使用urllib.request模塊執行此操作。請注意,HTML文檔只有一個標題,因此不需要使用find_all()和一個循環。

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()) 
+0

謝謝你有幫助:) – niloofar

+0

@niloofar很高興能幫到你! –