# -*- coding: UTF-8 -*-
import time
import uuid
import requests
import re
import json
import execjs


headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'From-Domain': '51job_web',
    'Pragma': 'no-cache',
    'Referer': 'https://we.51job.com/pc/search?keyword=java&searchType=2&sortType=0&metro=',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
    'account-id': '',
    'partner': '',
    'property': '%7B%22partner%22%3A%22%22%2C%22webId%22%3A2%2C%22fromdomain%22%3A%2251job_web%22%2C%22frompageUrl%22%3A%22https%3A%2F%2Fwe.51job.com%2F%22%2C%22pageUrl%22%3A%22https%3A%2F%2Fwe.51job.com%2Fpc%2Fsearch%3Fkeyword%3Djava%26searchType%3D2%26sortType%3D0%26metro%3D%22%2C%22identityType%22%3A%22%22%2C%22userType%22%3A%22%22%2C%22isLogin%22%3A%22%E5%90%A6%22%2C%22accountid%22%3A%22%22%7D',
    'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sign': '839932c059141791d8a003f0e6652e14facbf788a502df374fecf9c107d93b9e',
    'user-token': '',
    'uuid': '1687228791235576552',
}
params = {
    'api_key': '51job',
    'timestamp': '1769139097',
    'keyword': '华为技术有限公司',
    'searchType': '2',
    'function': '',
    'industry': '',
    'jobArea': '000000',
    'jobArea2': '',
    'landmark': '',
    'metro': '',
    'salary': '',
    'workYear': '',
    'degree': '',
    'companyType': '',
    'companySize': '',
    'jobType': '',
    'issueDate': '',
    'sortType': '0',
    'pageNum': '1',
    'requestId': '',
    'pageSize': '20',
    'source': '1',
    'accountId': '',
    'pageCode': 'sou|sou|soulb',
    'scene':'7'
}

# 换成自己的代理，或者不用，单个ip应该有限制
proxies = {
    "http":"http://xxx",
    "https":"http://xxxx"
}


for i in range(1,2):
    try:
        # cookie = {'guid': 'd02dfbabd84858301947663946e1710f'}
        session = requests.session()
        print("第%s次请求：" % i)
        response = session.get('https://we.51job.com/api/job/search-pc', params=params,headers=headers,verify=False)  # 关键：禁用SSL验证)
        print(response.text[:300])
        arg1 = re.findall("arg1='(.*?)';",response.text,re.S)[0]
        print('arg1--->',arg1)
        guid = str(uuid.uuid4()).replace("-", "")
        cookie = {'guid': str(guid)}

        with open('04.js', 'r', encoding='utf-8') as f:
            js = f.read()
        acw_sc__v2 = execjs.compile(js).call('l', arg1)
        print('acw_sc__v2-->',acw_sc__v2)
        cookie.update({"acw_sc__v2": acw_sc__v2})
        # cookie.update({"acw_sc__v3": "649257ebe376df87b3db6a94c1e5ad37f42f783b"})
        response2 = session.get('https://we.51job.com/api/job/search-pc', params=params,headers=headers,cookies=cookie,verify=False) #
        cookie.update(response2.cookies.get_dict())

        response = session.get('https://we.51job.com/api/job/search-pc', params=params,headers=headers, cookies=cookie,verify=False)
        print(response.text)
        time.sleep(0.5)
    except Exception as e:
        print(e)