Python and BeautifulSoup URL parse



我有以下代码,我正在使用这些代码尝试从redsox新闻中获取标题和描述。除了一个小细节,我有它的工作。它显示标签。我该如何消除它们?

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> 
>>> 

尝试:

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

你可以用BeautifulSoup做得更好,但这是让它发挥作用的快速方法。

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

这适用于

最新更新