3

WebAPIにクエリを実行してXML応答を受信するPython3スクリプトを作成しようとしています。応答は次のようになります– </ p>

<?xml version="1.0" encoding="UTF-8"?>
<ipinfo>
   <ip_address>4.2.2.2</ip_address>
   <ip_type>Mapped</ip_type>
   <anonymizer_status/>
   <Network>
      <organization>level 3 communications  inc.</organization>
      <OrganizationData>
     <home>false</home>
         <organization_type>Telecommunications</organization_type>
         <naics_code>518219</naics_code>
     <isic_code>J6311</isic_code>
      </OrganizationData>      
      <carrier>level 3 communications</carrier>
      <asn>3356</asn>
      <connection_type>tx</connection_type>
      <line_speed>high</line_speed>
      <ip_routing_type>fixed</ip_routing_type>
      <Domain>
         <tld>net</tld>
         <sld>bbnplanet</sld>
      </Domain>
   </Network>
   <Location>
      <continent>north america</continent>
      <CountryData>
         <country>united states</country>
         <country_code>us</country_code>
         <country_cf>99</country_cf>
      </CountryData>
      <region>southwest</region>
      <StateData>
         <state>california</state>
         <state_code>ca</state_code>
         <state_cf>88</state_cf>
      </StateData>
      <dma>803</dma>
      <msa>31100</msa>
      <CityData>
         <city>san juan capistrano</city>
         <postal_code>92675</postal_code>
         <time_zone>-8</time_zone>
         <area_code>949</area_code>
         <city_cf>77</city_cf>
      </CityData>
      <latitude>33.499</latitude>
      <longitude>-117.662</longitude>
   </Location>
</ipinfo>

これは私がこれまでに持っているコードです– </ p>

import urllib.request
import urllib.error 
import sys
import xml.etree.ElementTree as etree

…

try:
    xml = urllib.request.urlopen(targetURL, data=None)
except urllib.error.HTTPError as e:
    print("HTTP error: " + str(e) + " URL: " + targetURL)
    sys.exit()

tree = etree.parse(xml)
root = tree.getroot()

APIクエリは機能し、デバッガーを介して「root」変数内のすべての情報を確認できます。私の問題は<asn></asn>、返されたXMLからASN()のようなものを抽出する方法を理解できていないことです。私はこれに対して1日、さまざまな検索、検索、その他すべての種類の方法で頭を悩ませてきましたが、これを破ることはできませんでした。すべての木が木を見ることができず、インターネットで見つけたすべての例が役に立たないようになっていると思います。ツリー構造内からXML要素のコンテンツを抽出できるコードスニペットを誰かに見せてもらえますか?

どうもありがとう

ティム

4

1 に答える 1

-1

美しいスープの使用をお勧めします。

xmlコードからデータを抽出する場合は非常に強力です。

例:

from bs4 import BeautifulSoup
soup = BeautifulSoup(targetURL)

soup.find_all('asn') #Would return all the <asn></asn> tags found!
于 2012-09-21T10:13:36.567 に答える