From a64bb2f1b0b67f1c34277f09f69ffcb08e07aa74 Mon Sep 17 00:00:00 2001 From: prahul11 Date: Tue, 31 Oct 2023 01:56:27 +0530 Subject: [PATCH] jhg --- jobstreet/jst_malay_search2.py | 90 ++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 jobstreet/jst_malay_search2.py diff --git a/jobstreet/jst_malay_search2.py b/jobstreet/jst_malay_search2.py new file mode 100644 index 0000000..3cea48e --- /dev/null +++ b/jobstreet/jst_malay_search2.py @@ -0,0 +1,90 @@ +import requests +from math import ceil +from csv import DictWriter +from time import sleep + +def search_jst_malay(csv_file): + total_pages = 2 + current_page = 1 + url = 'https://xapi.supercharge-srp.co/job-search/graphql?country=my&isSmartSearch=true' + headers = { + 'Accept': '*/*', + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'en-US,en;q=0.9', + 'Content-Length': '3408', + 'Content-Type': 'application/json', + 'Origin': 'https://www.jobstreet.com.my', + 'Referer': 'https://www.jobstreet.com.my/', + 'Sec-Ch-Ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"', + 'Sec-Ch-Ua-Mobile': '?0', + 'Sec-Ch-Ua-Platform': '"Windows"', + 'Sec-Fetch-Dest': 'empty', + 'Sec-Fetch-Mode': 'cors', + 'Sec-Fetch-Site': 'cross-site', + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' + } + query = "query getJobs($country: String, $locale: String, $keyword: String, $createdAt: String, $jobFunctions: [Int], $categories: [String], $locations: [Int], $careerLevels: [Int], $minSalary: Int, $maxSalary: Int, $salaryType: Int, $candidateSalary: Int, $candidateSalaryCurrency: String, $datePosted: Int, $jobTypes: [Int], $workTypes: [String], $industries: [Int], $page: Int, $pageSize: Int, $companyId: String, $advertiserId: String, $userAgent: String, $accNums: Int, $subAccount: Int, $minEdu: Int, $maxEdu: Int, $edus: [Int], $minExp: Int, $maxExp: Int, $seo: String, $searchFields: String, $candidateId: ID, $isDesktop: Boolean, $isCompanySearch: Boolean, $sort: String, $sVi: String, $duplicates: String, $flight: String, $solVisitorId: String) {\n jobs(\n country: $country\n locale: $locale\n keyword: $keyword\n createdAt: $createdAt\n jobFunctions: $jobFunctions\n categories: $categories\n locations: $locations\n careerLevels: $careerLevels\n minSalary: $minSalary\n maxSalary: $maxSalary\n salaryType: $salaryType\n candidateSalary: $candidateSalary\n candidateSalaryCurrency: $candidateSalaryCurrency\n datePosted: $datePosted\n jobTypes: $jobTypes\n workTypes: $workTypes\n industries: $industries\n page: $page\n pageSize: $pageSize\n companyId: $companyId\n advertiserId: $advertiserId\n userAgent: $userAgent\n accNums: $accNums\n subAccount: $subAccount\n minEdu: $minEdu\n edus: $edus\n maxEdu: $maxEdu\n minExp: $minExp\n maxExp: $maxExp\n seo: $seo\n searchFields: $searchFields\n candidateId: $candidateId\n isDesktop: $isDesktop\n isCompanySearch: $isCompanySearch\n sort: $sort\n sVi: $sVi\n duplicates: $duplicates\n flight: $flight\n solVisitorId: $solVisitorId\n ) {\n total\n totalJobs\n relatedSearchKeywords {\n keywords\n type\n totalJobs\n }\n solMetadata\n suggestedEmployer {\n name\n totalJobs\n }\n queryParameters {\n key\n searchFields\n pageSize\n }\n experiments {\n flight\n }\n jobs {\n id\n adType\n sourceCountryCode\n isStandout\n companyMeta {\n id\n advertiserId\n isPrivate\n name\n logoUrl\n slug\n }\n jobTitle\n jobUrl\n jobTitleSlug\n description\n employmentTypes {\n code\n name\n }\n sellingPoints\n locations {\n code\n name\n slug\n children {\n code\n name\n slug\n }\n }\n categories {\n code\n name\n children {\n code\n name\n }\n }\n postingDuration\n postedAt\n salaryRange {\n currency\n max\n min\n period\n term\n }\n salaryVisible\n bannerUrl\n isClassified\n solMetadata\n }\n }\n}\n" + while current_page <= total_pages: + sleep(1) + variables = {"keyword":"", + "jobFunctions":[], + "locations":[], + "salaryType":1, + "jobTypes":[], + "createdAt":None, + "careerLevels":[], + "page": current_page, + "country":"my", + "sVi":"", + "solVisitorId":"7d3f7e5c-471e-411d-8a82-d8d29a303653", + "categories":[],"workTypes":[], + "userAgent":"Mozilla/5.0%20(Windows%20NT%2010.0;%20Win64;%20x64)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/118.0.0.0%20Safari/537.36", + "industries":[], + "locale":"en"} + data = { + 'query': query, + 'variables': variables + } + error_count = 0 + try: + response = requests.post(url, json=data, headers=headers) + if response.status_code == 200: + + # The request was successful + result = response.json() + # print(result['data']['jobs']['totalJobs']) + # print(type(result['data']['jobs']['totalJobs'])) + # print(result['data']['jobs']['totalJobs']/30) + # print(result['data']['jobs']['jobs']) + print('total pages', ceil(result['data']['jobs']['totalJobs']/30)) + total_pages = ceil(result['data']['jobs']['totalJobs']/30) + if len(result['data']['jobs']['jobs']) > 0 : + column = list(result['data']['jobs']['jobs'][0].keys()) + ['Company_Name'] + with open(csv_file, 'a+', newline='', encoding='utf-8') as csvfile: + writer = DictWriter(csvfile, fieldnames=column) + if csvfile.tell() == 0: + writer.writeheader() + for a_job in result['data']['jobs']['jobs']: + a_job['Company_Name'] = a_job['companyMeta']['name'] + writer.writerow(a_job) + + current_page += 1 + # total_pages -=1 + print(f"scraping page {current_page} of {total_pages}") + else: + + print(f"Request failed with status code {response.status_code}: {response.text}") + error_count +=1 + if error_count > 3: + current_page +=1 + # total_pages -=1 + except Exception as malayError: + print(malayError) + error_count+=1 + if error_count > 3: + current_page +=1 + # total_pages -=1 + + +if __name__ == "__main__": + search_jst_malay("testdata_jst_malay2.csv") \ No newline at end of file