2016-08-16 9 views
0

結果:美しいスープはのfindAllから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())' –

関連する問題