用 Python 导出 Glassdoor 薪资对照 CSV:同一职位下公司区间对城市基准
周五下午薪酬评审,业务方不会打开雇主主页。招聘同学小陈手里三个目标雇主——Google、Meta、Netflix——职位写死 software engineer,城市写死 san francisco。表上要有各公司最低 / 中位 / 最高,还要有城市基准那几列,否则「给高了还是给低了」没有尺子。评价、面试题、在招岗位链接都不进这张表;把某公司全部职级翻一遍也是另一份活。他要的就是一个文件,评审结束能直接丢进共享盘。
Glassdoor 数据 API 是非官方数据服务,返回公开可查、员工自行填报的数字,按请求当时拉取,不是对现薪的承诺,更不是 offer 计算器。打开本页或复制示例不会发出查询,只有你真正跑起来的请求才会。
登录后在控制台复制 Key,放到服务端环境变量 OPENDATA_API_KEY。文中的 YOUR_API_KEY 只是占位。Key 走查询参数,可能进访问日志,不要写进工单、截图和浏览器前端。
招聘真正会导出的对照表
产物就叫 compensation-comparison.csv。一行一个短名单雇主,列不要临时加。每行共用:职位、城市,以及 salary-estimate 给出的市场最低 / 中位 / 最高、市场币种和市场周期。雇主列:解析得到的 company_id、接口返回的公司名(不是你键盘上的别名)、company-salaries 的总包最低 / 中位 / 最高、币种、周期、员工报告数 salary_count、confidence。需要的话再加一列雇主 rating 当核对,别把它当成薪资。
只要三次调用,不是接口目录。company-search 把公司名变成稳定的 company_id;company-salaries 吃这个 id 加 job_title;salary-estimate 只要职位和地点,不需要公司。company-salaries-v2 是按页扫一家公司全部薪资报告,适合画分布、做职级盘点。那是另一张表:行是职位,不是雇主。这张对照表用不上它,也就不必为了「接口更全」多打一页。
职位、城市、可选的 years_of_experience 分档必须两侧一致。全国口径的公司数字对上城市市场,最容易做出假的「低于市场」幻灯片。一边传了 FOUR_TO_SIX,另一边也要传;一边收窄到 CITY,另一边不要默默落到全国。短名单里每个名字都按同一套参数打,表才对得上。
公司名只解析一次
公司侧接口认数字 company_id,不认你键盘上的字符串。每个名字搜一次、把 id 留下,短名单重复出现也走缓存。每次重搜既耗额度,也可能换成另一个头名——子公司、同名咨询公司、区域实体都会挤进相关度列表。
先看 HTTP:4xx / 5xx 是传输、鉴权或额度问题,请求没跑成,这时候才考虑换 Key、查配额。再看信封 {code, message, data}:code 为 200 只说明平台受理了,不说明搜到了公司。搜空时 data 是空数组——这个名字停掉,不要原地重试,也不要把空数组当成「稍后会有结果」。有命中时按相关度排序,优先精确匹配返回的 name;对不上就记下头名实际叫什么,写进 CSV,避免评审时发现对的是另一家。
def resolve_company_id(name, cache):
if name in cache:
return cache[name]
hits = call("company-search", query=name, limit=5) or []
chosen = None
for hit in hits:
if str(hit.get("name", "")).lower() == name.lower():
chosen = hit
break
if chosen is None and hits:
chosen = hits[0] # ranked fallback; store the name you actually got
cache[name] = chosen
return chosen
文档示例 query=google 常见头名 company_id 为 9079。多个品牌共用词根时优先精确匹配 name,CSV 里写入接口返回的名字,避免静默落到子公司。rating、salary_count、headquarters_location 适合当核对列,不必为此再打一次薪资接口。
单公司区间对上城市基准
company-salaries 一次只估一家公司的一个职位。必填 company_id 和 job_title。这张表还要带 location=san francisco、location_type=CITY,好跟市场行同一地理口径。location_type 还可以是 STATE、COUNTRY,但对照表一旦选定城市,就不要混用。
data 存在时是一条估值:总包 min_salary / median_salary / max_salary,另有基本工资 *_base_salary 和额外薪酬 *_additional_pay,以及 salary_period(例如 YEAR)、salary_currency、员工报告数 salary_count、confidence。表里用总包。不要拿 A 公司的基本工资去对 B 公司的总包,也不要把额外薪酬单独加进中位数——接口已经给过拆分,二次相加只会双计。开会引用中位数前先看样本量和置信度:上万条撑起来的宽区间,和三条样本撑起来的窄区间,不是同一种数。样本太少的行可以留在表里,但要让 reports、confidence 露出来,别只贴一个中位数字。
company_id 不存在时很闷:HTTP 200、信封 code 也是 200,但没有 data 字段。这是业务层未找到,不是可重试故障,跳过该行。不要把「没有 data」当成零薪入库。
salary-estimate 是尺子,不需要公司。同一套 job_title、location、location_type,整张表只打一次,把市场区间抄到每一行,避免三个雇主打三次市场、三次结果还对不上。data 缺失就让市场列空着,不要编一个基准,也不要用某一家公司的中位去冒充城市水平。先对齐 salary_period 和 salary_currency 再减中位数;时薪贴在年薪旁边不算溢价,不同币种更不能直接相减。
经验分档(LESS_THAN_ONE 到 ABOVE_FIFTEEN)两边一起收。非美国市场用 domain 切区域站,两侧同样要一致——英国站对美国站,口径会漂。
导出 CSV 的脚本
安装 requests,导出 OPENDATA_API_KEY,放在服务端跑。名字解析一次,每个雇主一条估值,城市基准一条,写出 compensation-comparison.csv。缺字段就空着,不要填 0 去冒充「没有薪酬」。短名单全空则不写文件,避免评审拿到只有表头的空表。雇主之间留一秒间隔,是客户端节奏,不是平台承诺的限额。
import csv
import os
import time
import requests
BASE = "https://api.opendata-api.com/api/project/glassdoor"
KEY = os.environ["OPENDATA_API_KEY"].strip()
JOB_TITLE = "software engineer"
LOCATION = "san francisco"
COMPANIES = ["google", "meta", "netflix"]
OUT = "compensation-comparison.csv"
def call(operation, **params):
resp = requests.get(
f"{BASE}/{operation}",
params={"key": KEY, **params},
timeout=30,
)
resp.raise_for_status() # HTTP layer: 4xx/5xx is transport or quota
envelope = resp.json()
if str(envelope.get("code")) != "200":
raise RuntimeError(envelope.get("message"))
return envelope.get("data") # may be missing, empty, or a payload
def resolve_company_id(name, cache):
if name in cache:
return cache[name]
hits = call("company-search", query=name, limit=5) or []
chosen = None
for hit in hits:
if str(hit.get("name", "")).lower() == name.lower():
chosen = hit
break
if chosen is None and hits:
chosen = hits[0]
cache[name] = chosen
return chosen
def main():
cache = {}
market = call(
"salary-estimate",
job_title=JOB_TITLE,
location=LOCATION,
location_type="CITY",
) or {}
rows = []
for name in COMPANIES:
hit = resolve_company_id(name, cache)
if not hit or hit.get("company_id") is None:
continue # empty search: skip, do not retry
company_id = str(hit.get("company_id"))
estimate = call(
"company-salaries",
company_id=company_id,
job_title=JOB_TITLE,
location=LOCATION,
location_type="CITY",
)
if not estimate:
# Unknown id: HTTP 200, gateway code 200, no data field
continue
rows.append({
"company": hit.get("name", name),
"company_id": company_id,
"rating": hit.get("rating"),
"job_title": JOB_TITLE,
"location": LOCATION,
"min_total": estimate.get("min_salary"),
"median_total": estimate.get("median_salary"),
"max_total": estimate.get("max_salary"),
"currency": estimate.get("salary_currency"),
"period": estimate.get("salary_period"),
"reports": estimate.get("salary_count"),
"confidence": estimate.get("confidence"),
"market_min": market.get("min_salary"),
"market_median": market.get("median_salary"),
"market_max": market.get("max_salary"),
"market_currency": market.get("salary_currency"),
"market_period": market.get("salary_period"),
})
time.sleep(1)
if not rows:
raise SystemExit("No employer rows to write")
with open(OUT, "w", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=list(rows[0].keys()))
writer.writeheader()
writer.writerows(rows)
if __name__ == "__main__":
main()
这张表就是交付物:同一职位、同一城市下,具名雇主对上一条市场基准,而不是接口清单。参数细节见 company-search、company-salaries 与 salary-estimate。