0

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())
9
  • is it possible that you are sending too many requests to the server? Commented Feb 6, 2022 at 0:12
  • could you show what response get for last line of text file also print responseid.text. my guess is perhaps the json happens to be an empty list for those Commented Feb 6, 2022 at 0:20
  • Please check that response status is also 200 or 201 so that the request was succesful. Are you sure that endpoint encoding is json ? Commented Feb 6, 2022 at 0:55
  • @AndrewRyan: i tried adding a wait between the consecutive calls to slow it down (went up to 15 seconds of wait), didn't help. Commented Feb 6, 2022 at 1:01
  • @MichałDarowny: i checked the response status within the for loop to confirm success, 200 every time. Commented Feb 6, 2022 at 1:02

1 Answer 1

1

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())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.