Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

api - How can I count all the jobs listing on the other pages in Python?

I'm trying to make an API call and count all the job postings from all pages. The thing is I only managed to get the count on first page and having trouble to make another API call for the next page. just to mention that there are maximum of 50 jobs per page. Thanks in advance!

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

def get_number_of_jobs(technology):
    number_of_jobs = 0
    page_count = 0
    tech = {'description': technology}
    response = requests.get(baseurl, params=tech)
    if response.ok:
        jobs = response.json()
        
    for job in jobs:
        for elm in job:
            if elm == 'id':
                number_of_jobs += 1
                if number_of_jobs > 49:
                    page_count += 1
                    tech = {'description': technology, 'page': page_count}
                    response = requests.get(baseurl, params=tech)
                    jobs = response.json()
                    number_of_jobs += 1


    return technology,number_of_jobs
question from:https://stackoverflow.com/questions/65865745/how-can-i-count-all-the-jobs-listing-on-the-other-pages-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you are looking for something like this, you can use len() instead of looping through and counting the "id" attribute.

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

def get_number_of_jobs(technology):

    job_count = 50
    total_job_count = 0
    page_count = 1
    
    while job_count > 49:
        job_count = 0
        # send request to GitHub for the next page
        tech = {'description': technology, 'page': page_count}
        response = requests.get(baseurl, params=tech)
        if response.ok:
            jobs = response.json()

            page_count += 1
            job_count = len(jobs)
            total_job_count += job_count

    return technology,total_job_count


print(get_number_of_jobs("java"))

The first call you make is page 1, so you would be calling page 1 twice in your algorithm, you can start at 1 and if > 49 continue to loop through.

Python result:

enter image description here

Github job posting for Java:

enter image description here

Let me know if you need anything clarifying,

Thanks,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...