Amostafa
Amostafa

Reputation: 79

GET Request in For Loop only returning response from the final loop index. Preceding ones return empty

I'm running get requests to a URL, using lines in a text file as arguments in said get requests. The issue I'm finding is that the responseind.json() is only returning the get request related to the LAST line of the txt file. All preceding lines return []. Code below, any ideas?

with open('industries.txt') as industry_file:
       for line in industry_file:
        start = time.time()
        responseind = requests.get("https:URL" + "".join(line) + "?token=My key")
        print(responseind.json())

Upvotes: 0

Views: 1020

Answers (1)

Andrew Ryan
Andrew Ryan

Reputation: 1611

The reason why your requests are not working properly is that you are just using the entire line in the text file (which ends in a new line character '\n'). The reason why your last request may be getting a response is that there is no new line after that id (the file ends) therefore not having that character which gives a valid id to look for.

with open('industries.txt') as industry_file:
    for line in industry_file:
        start = time.time()
        responseind = requests.get("https:URL" + "".join(line).replace('\n', '') + "?token=My key")
        print(responseind.json())

Upvotes: 1

Related Questions