Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8575605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:43:54+00:00 2026-06-11T19:43:54+00:00

I am trying to write a Python 3 script where I am querying a

  • 0

I am trying to write a Python 3 script where I am querying a web api and receiving an XML response. The response looks like this –

<?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>

This is the code I have so far –

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()

The API query works and through the debugger I can see all of the information inside the ‘root’ variable. My issue is that I have not been able to figure out how to extract something like the ASN (<asn></asn>) from the returned XML. I’ve been beating my head against this for a day with a whole wide variety of finds, findalls and all other sorts of methods but not been able to crack this. I think I have reached the point where I cannot see the wood for all the trees and every example I have found on the internet doesn’t seem to help. Can someone show me a code snippet which can extract the contents of a XML element from inside the tree structure?

Many thanks

Tim

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:43:55+00:00Added an answer on June 11, 2026 at 7:43 pm

    I would recommend using Beautiful Soup.

    It’s a very powerful when it comes to extracting data from xml-code.

    Example:

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(targetURL)
    
    soup.find_all('asn') #Would return all the <asn></asn> tags found!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a python script that takes record data like this
I am trying to write my very first python script. This was working but
I’m trying to write a python script that sends a query to TweetSentiments.com API.
I'm trying to write a python script that packages our software. This script needs
I'm trying to write a Python script that uses a particular external application belonging
I am trying to write a python script that will change my cwd to
I'm trying to write a python script that does the following from within a
I am trying to write a Python script that will access and modify the
I'm trying to write a Python script for searching out duplicate mp3/4 files using
I'm trying to write a simple Python script that inserts .odt documents into an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.