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( )
#3
Thanks for replying!

(Apr-21-2023, 02:51 AM)snippsat Wrote: You don't assigning to variable then loop,can do in one go when iterate over file object.
Example like this.

url_lst.csv:
Output:
https://www.google.com/, https://www.python.org/,
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

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.

Quote:Also when use an Api is most normal to work with Json return and not text.
results_text = r.text #Not normal 
result_json = r.json() #The normal way
when have Json can also use Serialization(get data back to original format eg a dictionary in this case).
with open("data.json", "w") as fp:
    json.dump(result_json, fp)
 
 with open("data.json") as fp_read:
    result_json = json.load(fp_read)

Yeah, that's largely what I'm doing in the code I have in the other part of the module. I was simply trying to pass each line of my input file as a variable out to the API using the r= results.get(My_URL_var) function.

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:

r=requests.get('https://MyTargetAPI.com/api/', params=payload)
I'm still up to my neck in it, but that seems to work for that portion...

Thanks, again!
-DBB
Reply


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

Forum Jump:

User Panel Messages

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