2016-03-22 3 views
2

У меня есть следующий код, который я использую, чтобы попытаться получить название и описание из новостей redsox. У меня есть работа, но для одной мелкой детали. показывая теги. Как я могу их устранить?Python и BeautifulSoup URL parse

import urllib2 
from BeautifulSoup import BeautifulSoup 
# or if you're using BeautifulSoup4: 
# from bs4 import BeautifulSoup 

soup = BeautifulSoup(urllib2.urlopen('http://partner.mlb.com/partnerxml/gen/news/rss/bos.xml').read()) 

title = soup.find('item').title 
desc = soup.find('item').description 

print "Title: %s " % (title) 
print "Summary: %s " % (desc) 

Это то, что он показывает

Title: <title>Shaw or Panda? Hot corner duel heats up</title> 
Summary: <description>With two weeks until Opening Day, the hottest topic in Red Sox camp is the competition at the hot corner between incumbent Pablo Sandoval and the emerging Travis Shaw.</description> 
>>> 
+0

http://stackoverflow.com/questions/16206380/python-beautifulsoup-how-to-remove-all-tags-from-an-element –

+0

Возможно, вы захотите ознакомиться с документами http://www.crummy.com/software/BeautifulSoup/bs4/doc/# get-text –

ответ

2

Try:

print "Title: %s " % (title.text) 
print "Summary: %s " % (desc.text) 

Вы можете сделать лучше с BeautifulSoup, но это быстрый способ сделать его работу.

+0

Благодарим вас за короткий, но сладкий ответ –

Смежные вопросы