无法访问的xml源条目

2024-04-29 04:16:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个python应用程序,它应该对电话簿搜索api发出请求,并格式化接收到的数据。 这些条目作为一个xml提要发送回来,看起来像底部的示例。你知道吗

我用feedparser来分割信息。你知道吗

我正在努力解决的是提取电子邮件字段。 这个信息包含在标签<tel:extra type="email">

我只能为最后一个额外条目获取“type”的值。你知道吗

前一个和标签之间的内容无法访问。你知道吗

有没有人有过这样的经验? 谢谢你帮助我。你知道吗

API information

Python代码

import feedparser
  data = feedparser.parse(xml)
  entry = data.entries[0]
  print(entry.tel_extra)

XML示例

<?xml version="1.0" encoding="utf-8" ?>
    <feed xml:lang="de" xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:tel="http://tel.search.ch/api/spec/result/1.0/">
      <id>https://tel.search.ch/api/04b361c38a40dc3aab2355d79f221f86/5acc2bdfc4554dfd5a4bb10424cd597e</id>
      <title type="text">tel.search.ch API Search Results</title>
      <generator version="1.0" uri="https://tel.search.ch">tel.search.ch</generator>
      <updated>2018-02-12T03:00:00Z</updated>
      <link href="https://tel.search.ch/result.html?was=nestle&amp;wo=broc&amp;private=0" rel="alternate" type="text/html" />
      <link href="http://tel.search.ch/api/?was=nestle&amp;wo=broc&amp;private=0&amp;key=04b361c38a40dc3aab2355d79f221f86" type="application/atom+xml" rel="self" />
      <openSearch:totalResults>1</openSearch:totalResults>
      <openSearch:startIndex>1</openSearch:startIndex>
      <openSearch:itemsPerPage>20</openSearch:itemsPerPage>
      <openSearch:Query role="request" searchTerms="nestle broc" startPage="1" />
      <openSearch:Image height="1" width="1" type="image/gif">https://www.search.ch/audit/CP/tel/de/api</openSearch:Image>
      <entry>
        <id>urn:uuid:ca71838ddcbb6a92</id>
        <updated>2018-02-12T03:00:00Z</updated>
        <published>2018-02-12T03:00:00Z</published>
        <title type="text">Nestlé Suisse SA</title>
        <content type="text">Nestlé Suisse SA
        Fabrique de Broc
        rue Jules Bellet 7
        1636 Broc/FR
        026 921 51 51</content>
        <tel:nopromo>*</tel:nopromo>
        <author>
          <name>tel.search.ch</name>
        </author>
        <link href="https://tel.search.ch/broc/rue-jules-bellet-7/nestle-suisse-sa" title="Details" rel="alternate" type="text/html" />
        <link href="https://tel.search.ch/vcard/Nestle-Suisse-SA.vcf?key=ca71838ddcbb6a92" type="text/x-vcard" title="VCard Download" rel="alternate" />
        <link href="https://tel.search.ch/edit/?id=ca71838ddcbb6a92" rel="edit" type="text/html" />
        <tel:pos>1</tel:pos>
        <tel:id>ca71838ddcbb6a92</tel:id>
        <tel:type>Organisation</tel:type>
        <tel:name>Nestlé Suisse SA</tel:name>
        <tel:occupation>Fabrique de Broc</tel:occupation>
        <tel:street>rue Jules Bellet</tel:street>
        <tel:streetno>7</tel:streetno>
        <tel:zip>1636</tel:zip>
        <tel:city>Broc</tel:city>
        <tel:canton>FR</tel:canton>
        <tel:country>fr</tel:country>
        <tel:category>Schokolade</tel:category>
        <tel:phone>+41269215151</tel:phone>
        <tel:extra type="Fax Service technique">+41269215154</tel:extra>
        <tel:extra type="Fax">+41269215525</tel:extra>
        <tel:extra type="Besichtigung">+41269215960</tel:extra>
        <tel:extra type="email">maisoncailler@nestle.com</tel:extra>
        <tel:extra type="website">http://www.cailler.ch</tel:extra>
        <tel:copyright>Daten: Swisscom Directories AG</tel:copyright>
      </entry>
    </feed>

Tags: texthttpsapiidhttpsearchtitletype
1条回答
网友
1楼 · 发布于 2024-04-29 04:16:27

你可能想去看看美苏。你知道吗

from bs4 import BeautifulSoup
soup = BeautifulSoup(xml, 'xml')

soup.find("tel:extra", attrs={"type":"email"}).text
Out[111]: 'maisoncailler@nestle.com'

相关问题 更多 >