簡體   English   中英

Python-無法使用LXML.HTML訪問某些標簽

[英]Python - Can't access some tags using LXML.HTML

改天又問一個問題,對不起所有帖子。 昨天,用戶“ JF Sebastian”給了我一個使用LXML.HTML的絕妙技巧,而不僅僅是使用LXML。

我今天將它用於另一個供稿http://feeds.bbc.co.uk/iplayer/search/tv/?q=news ,但我只是無法訪問content元素中的幾個標簽。

這是提要數據的示例:

  <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">
      &lt;p&gt;
    &lt;a href=&quot;http://www.bbc.co.uk/iplayer/episode/b01r27mt/BBC_News_at_Six_06_03_2013/&quot;&gt;
      &lt;img src=&quot;http://ichef.bbci.co.uk/programmeimages/episode/b01r27mt_150_84.jpg&quot; alt=&quot;BBC News at Six: 06/03/2013&quot; /&gt;
    &lt;/a&gt;
      &lt;/p&gt;
      &lt;p&gt;
    National and international news stories from the BBC News team, followed by weather.
      &lt;/p&gt;
    </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      &lt;p&gt;\n        &lt;a href=&quot;.+?&quot;&gt;\n          &lt;img src=&quot;(.+?)&quot; alt=&quot;.+?&quot; /&gt;\n        &lt;/a&gt;\n      &lt;/p&gt;\n      &lt;p&gt;\n     ').findall(content))
    #print "links: ", links
    #short=links
    #print "short: ", short

我想將帶有程序描述的第二個p標簽添加到上面的short變量中,但是我似乎無法使用lxml選擇此標簽,並且我無法讓regex來選擇所需的行。

有任何想法嗎?

您需要取消引用該文本以獲取html ,然后再次對其進行解析。

這里

from xml.sax import saxutils as su

unqoutedHtml = su.unescape(content)
newElement = html.document_fromstring(unqoutedHtml)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM