Python3 XML在标签之间获取文本

时间:2016-05-07 21:34:47

标签: python xml

我在Python 3中有以下代码。我使用import xml.etree.ElementTree as ET进行XML解析。 webScraper从webside获取文本,但在该网站上<link></link>标记之间有文本,但程序返回None。我可以看到程序找到了所有标签但是标签结果应该打印的地方只能说是None。

    result = webScrapper.scrappPart("http://www.dn.se/rss/senaste-nytt/", "body")
    root = ET.fromstring(result)
    for items in root.findall('.//item'):
        link = items.find('link')
        print(link.text)

有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

由于您的网址实际上是RSS提要,因此您可以更好地使用RSS提要解析器,而不是尝试自己推送。幸运的是,这就是feedparser存在的原因。看看这个:

<div id="pages">
    <!-- apply page -->
    <div id="apply" class="sections" tabindex='2'>
        <!-- navigation bar -->
        <div id="navbar">
            <ul id="navbar-links">
                <li>Apply</li>
                <li>About</li>
                <li>Sponsor</li>
            </ul>
        </div>
        <div class="container">

        </div>  
    </div>

    <!-- about page -->
    <div id="about" class="sections" tabindex='3'>
        <div class="container">

        </div>        
    </div>

    <!-- sponsor page -->
    <div id="sponsor" class="sections" tabindex='4'>
        <div class="container">

        </div>        
    </div>
</div>

返回

import feedparser as fp

feed = fp.parse("http://www.dn.se/rss/senaste-nytt/")
for entry in feed["entries"]:
    print(entry["link"])

我认为是你正在寻找的东西。

答案 1 :(得分:1)

你可以使用ElementTree就好了,你只需要传递源并使用xpath:

from xml.etree import ElementTree as et
import requests

tree = et.fromstring(requests.get("http://www.dn.se/rss/senaste-nytt/").content)

print([x.text for x in tree.findall(".//item//link")])

输出:

['http://www.dn.se/nyheter/varlden/andlig-ledare-ihjalhackad-i-bangladesh/', 'http://www.dn.se/nyheter/sverige/tillstandet-battre-for-pakord-ettaring/', 'http://www.dn.se/ekonomi/maria-crofts-dags-att-gora-nagot-at-orattvisa-pensioner/', 'http://www.dn.se/nyheter/varlden/turkisk-militar-dodade-55-is-krigare/', 'http://www.dn.se/nyheter/varlden/massiv-fiskdod-i-sjo/', 'http://www.dn.se/nyheter/varlden/kanadabranden-i-bilder/', 'http://www.dn.se/nyheter/sverige/manga-saknas-efter-jordskred-i-kina/', 'http://www.dn.se/nyheter/sverige/fortsatt-sokande-efter-student/', 'http://www.dn.se/nyheter/sverige/en-dod-i-villabrand-8/', 'http://www.dn.se/nyheter/politik/v-vill-ta-bort-terrorstampel-pa-pkk/', 'http://www.dn.se/ekonomi/raknehjalp-pa-natet-ger-ratt-underhall/', 'http://www.dn.se/nyheter/varlden/kanadabranden-fullstandigt-okontrollerad/', 'http://www.dn.se/nyheter/varlden/attentat-mot-journalister-besvarande-for-erdogan/', 'http://www.dn.se/nyheter/varlden/superlobbyist-ska-gora-trump-serios/', 'http://www.dn.se/nyheter/vetenskap/karin-bojs-en-typisk-foralder-ar-28-ar-gammal/', 'http://www.dn.se/sport/nervos-vantan-pa-em-biljetter/', 'http://www.dn.se/ekonomi/ovantat-stort-exportfall-i-kina/', 'http://www.dn.se/ekonomi/lott-gav-35-miljarder-i-vinst-i-usa/', 'http://www.dn.se/nyheter/vetenskap/fabels-kansliga-nos-ska-ge-svar-om-massmordet/', 'http://www.dn.se/sport/johan-esk-nu-borde-idrotten-lara-ledarskap-av-naringslivet-1/', 'http://www.dn.se/sport/melker-karlsson-malskytt-for-san-jose/', 'http://www.dn.se/sport/backstrom-visade-vagen-till-washingtons-viktiga-vinst/', 'http://www.dn.se/nyheter/varlden/15-miljoner-signaturer-bekraftade/', 'http://www.dn.se/nyheter/varlden/medan-du-sov-varlden-i-korthet-8-maj-1/', 'http://www.dn.se/nyheter/varlden/karnvapen-bara-om-landet-hotas/', 'http://www.dn.se/nyheter/varlden/protester-mot-att-avskaffa-senat/', 'http://www.dn.se/nyheter/sverige/industri-brann-i-uppsala/', 'http://www.dn.se/nyheter/varlden/atta-poliser-dodade-i-attack/', 'http://www.dn.se/nyheter/varlden/tva-fast-vid-myr-utanfor-kiruna/', 'http://www.dn.se/sport/hockeyhasten-nyquist-vann-kentucky-derby/', 'http://www.dn.se/nyheter/varlden/15-miljoner-signaturer-bekraftade-i-venezuela/']

或者使用lxml,它也可以为您提供源代码:

from lxml import etree

result = etree.parse("http://www.dn.se/rss/senaste-nytt/")
print(result.xpath("//item//link//text()"))

它为您提供完全相同的输出。