I have never used Python before or made a call to an API so I am a little lost. I need to send an address to https://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.html#_Toc143583220 and get back the latitude and longitude. All of the help I could find was about addressbatch which I don't need.
The address I am using is a random one given on the command line :)
Here's what I have (which is very much not working) How do I get the api to return the lat and long?
def main():
#reading URL input
urlIn = sys.argv[1]
IP = socket.gethostbyname(urlIn)
res = subprocess.getstatusoutput("whois " + IP)
ressplit = res[1].split("\n")
#parsing address from result
for i in ressplit:
if i.startswith("Address: "):
a = i[10:]
if i.startswith("City: "):
c = i[6:]
if i.startswith("StateProv: "):
s = i[11:]
if i.startswith("PostalCode: "):
p = i[12:]
break
#combining to make address
address = a.strip() + ", " + c.strip() + ", " + s.strip() + " " + p.strip()
print(address)
#base API string for geocoding.gov
geo_s = "https://geocoding.geo.census.gov/geocoder/locations/address?"
location = address
PARAMS = {'address':location}
response = requests.get(url = geo_s, params = PARAMS)
data = response.json()
#convert it to json
geojs = json.loads(response.text)
latitude = data['x']
longitude = data ['y']
and what it looks like from the terminal:
cscd330lab2 % ./lab2.py "youtube.com"
1600 Amphitheatre Parkway, Mountain View, CA 94043
Traceback (most recent call last):
File "/Users/emilyherbert/Library/Python/3.9/lib/python/site-packages/requests/models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/emilyherbert/Desktop/cscd330lab2/./lab2.py", line 82, in <module>
main()
File "/Users/emilyherbert/Desktop/cscd330lab2/./lab2.py", line 40, in main
data = response.json()
File "/Users/emilyherbert/Library/Python/3.9/lib/python/site-packages/requests/models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)