jdm3 0 Newbie Poster

Hello, I'm currently working on a Python CGi program. This program takes a zipcode in a form and interacts with a website to obtain information on the Congress representative for that district. My question is, certain zipcodes have multiple representatives, and I want to make a page with radio buttons for the user to select which representative they want, if the zipcode they give has multiple representatives. I was curious if anyone has any insight on how I can make this step if needed, or have it bypassed like my current code does and go directly to my CGI page displaying the extracted information.

Below is the piece of code I have so far that takes and uses the zip entered into the form. This code runs fine and has no errors, I would just like to have that radio button option for certain zipcodes. (Python 3)

Thanks in advance for any help!


import cgi
form = cgi.FieldStorage()
import xml.etree.ElementTree as ET
import urllib.request as u
zipc = form.getfirst('val', 'Enter Zipcode')
url = "http://watchdog.net/us/?zip="
conn = u.urlopen(url+zipc)
cont = conn.readlines()

#Read content from Watchdog and extract name of politician
#Read through source and extract state and district
name =''
state =''
district =''
for lines in cont:
    line = lines.decode("utf-8")
    start= line.find('/p/')          #find name to extract 
    end= line.find('">')
    name += line[start:end]
    start1= line.find('state=')      #find and extract state
    end1= line.find('&')
    state += line[start1:end1]
    start2= line.find('district=')   #find and extract district
    end2= line.find('" ')
    district += line[start2:end2]

#Browse and extract data from XML file
url= "http://watchdog.net"
file = name + ".xml"
root = ET.parse(u.urlopen(url+file))
for elem in root.getiterator():
    if elem.tag == "{http://watchdog.net/about/api#}firstname":  
        fname = elem.text
    if elem.tag == "{http://watchdog.net/about/api#}lastname":    #extract first/last name
        lname = elem.text
    if elem.tag == "{http://watchdog.net/about/api#}gender":      #extract gender
        gen = elem.text
    if elem.tag == "{http://watchdog.net/about/api#}party":       #extract party
        party = elem.text
    if elem.tag == "{http://watchdog.net/about/api#}birthplace":  #extract birthplace
        bplace = elem.text
    if elem.tag == "{http://watchdog.net/about/api#}birthday":    #extract birthday
        bday = elem.text
    if elem.tag == "{http://watchdog.net/about/api#}govtrackid":  #extract attribute
        gov = elem.attrib
        strip = gov['{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource']
        govid = strip[-6:]      #extract last 6 characters of definition after obtaining with dict key

#insert govid to grab and print picture of politician 
#sets image to center aligned and to be specific size
img = '<img src ="http://www.govtrack.us/data/photos/%s.jpeg" align="middle" width="200" height="250">' % govid

#Print all necessary information
print("<br />"+"Name:", fname, lname +"<br />") 
print("Gender:",gen +"<br />")
print("Party:",party +"<br />")
print("Birthplace:",bplace +"<br />")
print("<br />"+"State and District:", state[-2:], district.title())
print("<br />"+"Government ID:", govid)