ritulit
ritulit

Reputation: 1

python API paging loops becomes infinite

I cant understand why this while loop is infinite(it seems so cause it just gets stuck) It increments the page number and supposed to stop the requests once it gets an empty body (no more results). I check the body with the length of the response body

baseurl = "https://jobs.github.com/positions.json"

def get_number_of_jobs(technology):
    number_of_jobs = 0
    page=1
    payload={"description":technology,"page":page}
    new_results=1
    while new_results>0:
        r=requests.get(baseurl,payload)
        new_results =len(r.json())
        page+=1
        number_of_jobs+=(len(r.json()))
         
    return technology,number_of_jobs

Upvotes: 0

Views: 472

Answers (2)

Gabe
Gabe

Reputation: 173

The loop is infinite because the condition new_results > 0 is always true.

And as @DYZ noted, the payload variable never changes, so you're perpetually requesting the same page, therefore no values change in each iteration.

Upvotes: 0

David Delač
David Delač

Reputation: 367

Your payload={"description":"php","page":page} should be inside the while loop because it has page that is otherwise always 1.

Your code should be this:

baseurl = "https://jobs.github.com/positions.json"

def get_number_of_jobs(technology):
    number_of_jobs = 0
    page=1
    new_results=1
    while new_results>0:
        payload={"description":technology,"page":page}
        r=requests.get(baseurl,payload)
        new_results =len(r.json())
        page+=1
        number_of_jobs+=(len(r.json()))
         
    return technology,number_of_jobs

Upvotes: 2

Related Questions