2016-08-16 33 views
0

我有這樣的代碼:美麗的湯刪除HTML標記導致

import requests 
from bs4 import BeautifulSoup 

url = "https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-today.aspx?sign=1" 
page = requests.get(url) 


soup = BeautifulSoup(page.text, "html.parser") 

horoscope = soup.findAll("div", {"class": "block-horoscope-text f16 l20"}, text=True) 

,但返回的結果中包含的標籤爲好。

<div class="block-horoscope-text f16 l20"> 
      It could be scary for you to do anything risky for fear of conflict or failure, Aries. Perhaps you've tried to become invisible in different situations so you can avoid being noticed. These defense mechanisms may serve you for a while, but acting out of fear or guilt won't get you where you need to go. To achieve what you want, you must act with confidence, love, and faith. 
     </div> 

如何將其刪除?謝謝你的幫助。

回答

1

只需加上[0].text,希望對您有所幫助!

horoscope = soup.findAll("div", {"class": "block-horoscope-text f16 l20"}, text=True)[0].text 
print(horoscope) 
+0

它的工作原理。謝謝。 – CodesInTheValley

+0

歡迎:) @CodesInTheValley –

+0

如果你想消除空格,'print(str(horoscope).strip())' –