Python Forum
Using a String var as URL in Requests.get( )
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a String var as URL in Requests.get( )
#4
(Apr-21-2023, 11:06 PM)DBB Wrote: I'm not sure I follow the above. To be clear, my input file already is a collection of completely formed URLs that I'm trying to pass to the API, one by one as I step through that file.
That what's my code do,if i take your url it would use that in last in requests.get().
Output:
https://www.google.com/, https://www.python.org/, 'https://MyTargetAPI.com/api/?firstname=bozo&lastname=clown&state=FL
import csv
import requests
 
with open('url_lst.csv') as fp:
    for url in csv.reader(fp):
        print(url[0])
        response = requests.get(url[0])
        print(response.status_code)
Output:
https://www.google.com/ 200 https://www.python.org/ 200 'https://MyTargetAPI.com/api/?firstname=bozo&lastname=clown&state=FL Traceback (most recent call last): ..... requests.exceptions.InvalidSchema: No connection adapters were found for "'https://MyTargetAPI.com/api/?firstname=bozo&lastname=clown&state=FL"
So this would work if it had been a working url(know that is just example) it would give 200 back,and then could use respone.json or respone.text to get return from Api.

Quote:I have scoured the internet looking for a method of doing that, so maybe it's not possible. I ended up reworking my input .csv file to only include the items I needed to include as parameters in the URL string, then created a payload variable to include in the requests.get() function like this
Could split it up,but you mention that is was completely formed URL'S than a a loop as showed should work.
Reply


Messages In This Thread
RE: Using a String var as URL in Requests.get( ) - by snippsat - Apr-22-2023, 07:33 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020