1
另一天的另一个问题,对所有帖子感到抱歉。昨天,用户“J.F. Sebastian”给了我一个很好的建议,使用LXML.HTML,而不是仅仅使用LXML。Python - 无法使用LXML访问某些标签.HTML
今天我使用它作为另一个饲料http://feeds.bbc.co.uk/iplayer/search/tv/?q=news
,但我只是无法访问内容元素中的一对标签。
这里是饲料的数据样本:
<entry>
<title type="text">BBC News at Six: 06/03/2013</title>
<id>tag:feeds.bbc.co.uk,2008:PIPS:b01r27mt</id>
<updated>2013-03-07T00:20:38Z</updated>
<content type="html">
<p>
<a href="http://www.bbc.co.uk/iplayer/episode/b01r27mt/BBC_News_at_Six_06_03_2013/">
<img src="http://ichef.bbci.co.uk/programmeimages/episode/b01r27mt_150_84.jpg" alt="BBC News at Six: 06/03/2013" />
</a>
</p>
<p>
National and international news stories from the BBC News team, followed by weather.
</p>
</content>
<category term="News" />
<category term="TV" />
<link rel="alternate" href="http://www.bbc.co.uk/iplayer/episode/b01r27mt/BBC_News_at_Six_06_03_2013/" type="text/html" title="BBC News at Six: 06/03/2013">
<media:content>
<media:thumbnail url="http://ichef.bbci.co.uk/programmeimages/episode/b01r27mt_150_84.jpg" width="150" height="84" />
</media:content>
</link>
<link rel="self" href="http://feeds.bbc.co.uk/iplayer/episode/b01r27mt" type="application/atom+xml" title="06/03/2013" />
<link rel="related" href="http://www.bbc.co.uk/programmes/b007mpkn/microsite" type="text/html" title="BBC News at Six" />
</entry>
看来,内容标签内的标签文字和没有得到正确的解析。这里是我的代码:
tree = html.parse("http://feeds.bbc.co.uk/iplayer/search/tv/?q=news")
for show in tree.xpath('//entry'):
select = lambda expr: show.cssselect(expr)[0]
icon_url=select("thumbnail").get('url')
print "icon_url: ", icon_url
name=select('title').text_content()
print "name: ", name
stream=select('id').text_content()
print "stream: ", stream
date=select('updated').text_content()
print "date: ", date
content=select('content').text_content()
print "content: ", content
#links = (re.compile ('\n <p>\n <a href=".+?">\n <img src="(.+?)" alt=".+?" />\n </a>\n </p>\n <p>\n ').findall(content))
#print "links: ", links
#short=links
#print "short: ", short
我想与程序描述成以上的短期变量的第二p标签,但我似乎不能够使用LXML选择这个标签,我不能让正则表达式来选择我想要的行..
任何想法?