简体   繁体   中英

How can I determine number of jobs for a given technology from a list I have obtained in .json format from GitHub job opening API in python?

My task is to Write a function to get the number of jobs for the given technology. Note: The API gives a maximum of 50 jobs per page. If you get 50 jobs per page, it means there could be some more job listings available. So if you get 50 jobs per page you should make another API call for next page to check for more jobs. If you get less than 50 jobs per page, you can take it as the final count.

Following is my code

baseurl = "https://jobs.github.com/positions.json"
def get_number_of_jobs(technology):
    number_of_jobs = 0
    tech = technology
    page= 0
    PARAMS  = {'technology':tech , 'page': page}
    jobs=requests.get(url=baseurl,params = PARAMS )
    if jobs.ok:             
        listings = jobs.json()
    number_of_jobs=len(listings)
    if number_of_jobs==50:
        page= page+1 
        PARAMS  = {'technology':tech , 'page': page}
        jobs=requests.get(url=baseurl,params = PARAMS )
        if jobs.ok:             
            listings2 = jobs.json()
    number_of_jobs= number_of_jobs + len(listings2)
    return technology,number_of_jobs

Now I can not figure out how to do the pagination in this function? Meaning how to check if there are more than 50 job posting for a specific technology or not and if it is then run the code again and get those postings as well?

I print the output as

print(get_number_of_jobs('python'))

('python', 100) 

Can someone please help?

Many thanks in advance!

Please let me know if should work

import requests
baseurl = 'https://jobs.github.com/positions.json'

total_job = 0

def get_number_of_jobs(technology, page):
    global total_job
    PARAMS  = {'technology':technology , 'page': page}
    jobs=requests.get(url=baseurl,params = PARAMS )
    total_job += len(jobs.json()) if jobs.ok else 0
    return len(jobs.json()) if jobs.ok else 0

def get_jobs(technology):
    page = 0
    while get_number_of_jobs(technology, page) >= 50:page+=1
    return total_job       


print(get_jobs('python'))
baseurl = 'https://jobs.github.com/positions.json'    
def get_number_of_jobs(technology):
    number_of_jobs = 0
    page = 0
    while True:
      payload = {"description":technology,"page":page}
      r = requests.get(baseurl,params=payload)
      if r.ok:
        data = r.json()
      number_of_jobs = len(data)
      if number_of_jobs >= 50:
        page += 1
        continue
      else:
        break    
    return technology,number_of_jobs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM