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( )
#1
New to python so forgive me...

I have an external .csv file containing several lines, each one of which is a fully formed URL, having user names and states as parameters, eg:
'https://MyTargetAPI.com/api/?firstname=bozo&lastname=clown&state=FL'

I'm trying to iterate through the list of URLs and pass each one through the requests.get() function, with the retrieved results saved to a file.

My module is working for one record when the actual URL is used in the requests.get() function, thusly:

r = requests.get('https://MyTargetAPI.com/api/?firstname=bozo&lastname=clown&state=FL')
results_text = r.text
print(results_text)   #displays the results returned from the API
The rest of my code correctly handles the information returned from the API and saves it to file, as intended.

But I have had no luck attempting to pull each URL from the file, assigning it to a variable, then using the variable in the requests.get() function.

I can iterate through each line of the external file and the print(line.strip()) section correctly displays each retrieved URL as I loop through. But if I attempt to assign the retrieved URL string to a variable, then use the variable in the requests.get() function rather than the actual URL, things go south:

#By itself, this seems to be working, and displays each URL retrieved as I loop through the external file:
print(line.strip())
     
#But when I follow on with this, it breaks:
api_url = line.strip()
r = requests.get(api_url)
results_text = r.text
print(results_text) 
I'm not sure of the syntax, and so this is probably something obvious to you all, but I'm not sure how to de-reference the variable within the function to get this working. I would really appreciate a nudge in the right direction.

-DBB
Reply


Messages In This Thread
Using a String var as URL in Requests.get( ) - by DBB - Apr-20-2023, 11:52 PM

Forum Jump:

User Panel Messages

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