import requests
import json
api_url = "https://api.taxes.info.net/taxes/%s/rate?page=1"
list =[
{"state": "Ohio",
"state": "California",
"state": "Georgia"}
]
for i in list:
state_name = i.get("state")
urls = api_url % state_name
print(urls) returns: >>>>> https://api.taxes.info.net/taxes/Ohio/rate?page=1 https://api.taxes.info.net/taxes/California/rate?page=1 https://api.taxes.info.net/taxes/Georgia/rate?page=1
for url in urls:
response = requests.get(url, headers={"Accept": "state.api jsonv3", "Authorization": "password1234")
Getting error message: >>>>> requests.exceptions.MissingSchema: Invalid URL 'h': No schema supplied. Perhaps you meant http://h?
I'm trying to pass down the urls stored in my first for loop and pass it down to my API request. I'm fairly new to programming so any suggestions or pointers would be greatly appreciated. I did confirm my indentation is correct in my second for loop.
CodePudding user response:
In your first loop you create a variable urls which is an string and should represent a url. After that you're doing another loop inside the first loop and in this one you go trough every letter in this urls variabe. The error message you get is because you try to make a request to 'h'. Your code should look like that:
import requests
import json
api_url = "https://api.taxes.info.net/taxes/%s/rate?page=1"
list = [
{"state": "Ohio",
"state": "California",
"state": "Georgia"}
]
urls = []
for i in list:
state_name = i.get("state")
urls.append(api_url % state_name)
print(urls)
for url in urls:
response = requests.get(url, headers={"Accept": "state.api jsonv3", "Authorization": "password1234"})
CodePudding user response:
Let's go in order.
You have created a list of one dict. Although in your case, you need to create an array and list the necessary elements in it.
list =[
{"state": "Ohio",
"state": "California",
"state": "Georgia"}
]
further in the loop, you create the urls variable, and it seems to be correct, but you redefine it. This means that if you have 100 elements, then the variable will still have the last one.
for i in list:
state_name = i.get("state")
urls = api_url % state_name
And since your urls is a string, then looping through it, the python will iterate each character, which is exactly what requests complain about.
Below I have attached the code which should work correctly import requests import json
api_url = "https://api.taxes.info.net/taxes/%s/rate?page=1"
states = ["Ohio", "California", "Georgia"]
request_urls = []
for state in states:
state_name = state
request_urls.append(api_url % state_name)
for url in request_urls:
response = requests.get(url, headers={"Accept": "state.api jsonv3", "Authorization": "password1234"})
CodePudding user response:
TL;DR The reason you are getting this error is because urls
is a string and not a list (might I suggest renaming it to url
). This means the second for loop is unnecessary!
If you would like a more in depth response:
If you set the variables urls
to a string (let's say https://api.taxes.info.net/taxes/Georgia/rate?page=1
) you don't need to iterate through it. In fact you can just pass that string to requests.get
like so:
for i in list:
state_name = i.get("state")
urls = api_url % state_name
# This works fine.
response = requests.get(urls, headers={"Accept": "state.api jsonv3", "Authorization": "password1234"})
Now you may be wondering why you are getting the error Invalid URL 'h': No schema supplied.
This is because in python you can actually iterate through a string (which is what you are doing).
for l in "https://google.com":
print(l)
This will print every letter in https://google.com
in order. So when you pass in the first letter of your URL to the requests module, it gets confused because h
is not a URL (first letter of https://api.taxes.info.net/taxes/Georgia/rate?page=1
)
In conclusion
Instead of iterating through a string, you should just pass that string to requests.get
directly
Hope this helped at least a little.
This is my first stack overflow answer so any feedback is welcome!