|
| 1 | +import requests |
| 2 | +import xlwt |
| 3 | +from xlwt import Workbook |
| 4 | + |
| 5 | +BASE_URL = 'https://remoteok.com/api' |
| 6 | +USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36' |
| 7 | +REQUEST_HEADER = { |
| 8 | + 'User-Agent': USER_AGENT, |
| 9 | + 'Accept-Language': 'en-US, en;q=0.5', |
| 10 | +} |
| 11 | + |
| 12 | +def get_job_postings(): |
| 13 | + """Fetch job postings from RemoteOK API.""" |
| 14 | + try: |
| 15 | + res = requests.get(BASE_URL, headers=REQUEST_HEADER) |
| 16 | + res.raise_for_status() |
| 17 | + data = res.json() |
| 18 | + return data[1:] |
| 19 | + except requests.RequestException as e: |
| 20 | + print("Error fetching jobs:", e) |
| 21 | + return [] |
| 22 | + |
| 23 | +def save_jobs_to_excel(jobs, filename='remoteok_jobs.xls'): |
| 24 | + """Save job postings to an Excel file.""" |
| 25 | + if not jobs: |
| 26 | + print("No job data to save.") |
| 27 | + return |
| 28 | + |
| 29 | + wb = Workbook() |
| 30 | + sheet = wb.add_sheet('Jobs') |
| 31 | + |
| 32 | + headers = list(jobs[0].keys()) |
| 33 | + for col, header in enumerate(headers): |
| 34 | + sheet.write(0, col, header) |
| 35 | + |
| 36 | + for row, job in enumerate(jobs, start=1): |
| 37 | + for col, key in enumerate(headers): |
| 38 | + sheet.write(row, col, str(job.get(key, ''))) |
| 39 | + |
| 40 | + wb.save(filename) |
| 41 | + print(f"Jobs saved to {filename}") |
| 42 | + |
| 43 | +if __name__ == '__main__': |
| 44 | + jobs = get_job_postings() |
| 45 | + save_jobs_to_excel(jobs) |
0 commit comments