简体   繁体   中英

Call an API multiple times with a different URL in Python

I'm using Zillow's API and making a call to the GetDeepComps API. The API allows you to include an address in the URL, then returns a response.

I want to send multiple requests to the API by reading a text file full of x addresses and then calling the API x times, until there are no more addresses remaining in the file.

The value of the variable formatted_addresses should change depending on the line being read in the text file containing the addresses.

I also want to store the address and its corresponding zip code in a dictionary. Here's my current code.

def read_addresses_and_append_zip_codes():
    f = open("addresses.txt", "r")
    addresses = f.readlines()
    addresses = [x.strip() for x in addresses]
    print addresses
    formatted_address = "2723+Green+Leaf+Way"
    DEEP_SEARCH_RESULTS_BASE_URL = "http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=" + API_KEY + "&address=" + formatted_address + "&citystatezip=San%20Antonio%2C%20TX"
    response = requests.get(DEEP_SEARCH_RESULTS_BASE_URL)
    content = xmltodict.parse(response.content)
    zip_code = content['SearchResults:searchresults']['response']['results']['result']['address']['zipcode']
    print zip_code


read_addresses_and_append_zip_codes()

What's a good way to do this?

I would define some base url like this

BASE_URL = "http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id={api_key}&address={encoded_addr}&citystatezip=San%20Antonio%2C%20TX"

then, you can insert the API_KEY and formatted_address using str.format() like this

new_url = BASE_URL.format(**{'api_key': API_KEY, 'encoded_addr': encoded_addr})

where we define

import urllib
encoded_addr = urllib.quote_plus(addr)

Then the whole thing would look something like this:

def read_addresses_and_append_zip_codes():
    zips = {}
    BASE_URL = "http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id={0}&address={1}&citystatezip=San%20Antonio%2C%20TX"
    with open("addresses.txt", "r") as f:
        addresses = f.readlines()
        addresses = [x.strip() for x in addresses]
        # print addresses
        for addr in addresses:
            encoded_addr = urlparse.quote_plus(addr)
            response = requests.get(BASE_URL.format(**{'api_key': API_KEY, 'encoded_addr': encoded_addr}))
            content = xmltodict.parse(response.content)
            zip_code = content['SearchResults:searchresults']['response']['results']['result']['address']['zipcode']
            zips[addr] = zip_code


read_addresses_and_append_zip_codes()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM