如何通过Upwork GraphQL API获取全部或指定数量的筛选职位?
解决Upwork GraphQL API获取更多职位的问题
要获取超过默认10个的职位结果,你需要利用GraphQL的分页参数,具体操作如下:
1. 单次请求获取指定数量(如前100个)职位
Upwork的marketplaceJobPostingsSearch查询支持first参数,用来指定单次返回的职位数量(需注意API有单次返回上限,通常最大为100)。你需要修改查询语句和变量,添加该参数:
修改后的完整请求
{"query": "query marketplaceJobPostingsSearch($marketPlaceJobFilter: MarketplaceJobPostingsSearchFilter, $searchType: MarketplaceJobPostingSearchType, $sortAttributes: [MarketplaceJobPostingSearchSortAttribute], $first: Int) { marketplaceJobPostingsSearch(marketPlaceJobFilter: $marketPlaceJobFilter, searchType: $searchType, sortAttributes: $sortAttributes, first: $first) { totalCount edges { node { id title description category } } pageInfo { hasNextPage endCursor } } }","variables": {"marketPlaceJobFilter": {"titleExpression_eq": "JAVA"},"searchType": "USER_JOBS_SEARCH","sortAttributes": [{ "field": "RECENCY" }],"first": 100}}
关键修改点
- 在查询的参数定义里新增
$first: Int - 在
marketplaceJobPostingsSearch调用时传入first: $first - 在variables里添加
"first": 100,指定单次返回100个结果
2. 获取符合条件的全部职位
如果结果总数超过单次请求上限,需要通过游标分页循环获取:
- 第一次请求使用上述带
first参数的查询,拿到返回结果里的pageInfo.endCursor和pageInfo.hasNextPage - 如果
hasNextPage为true,则发起下一次请求,在查询里新增$after: String参数,调用marketplaceJobPostingsSearch时传入after: $after,并在variables里设置"after": "上一次的endCursor值" - 重复上述步骤,直到
hasNextPage变为false,此时已获取全部结果
分页请求示例(第二次请求)
{"query": "query marketplaceJobPostingsSearch($marketPlaceJobFilter: MarketplaceJobPostingsSearchFilter, $searchType: MarketplaceJobPostingSearchType, $sortAttributes: [MarketplaceJobPostingSearchSortAttribute], $first: Int, $after: String) { marketplaceJobPostingsSearch(marketPlaceJobFilter: $marketPlaceJobFilter, searchType: $searchType, sortAttributes: $sortAttributes, first: $first, after: $after) { totalCount edges { node { id title description category } } pageInfo { hasNextPage endCursor } } }","variables": {"marketPlaceJobFilter": {"titleExpression_eq": "JAVA"},"searchType": "USER_JOBS_SEARCH","sortAttributes": [{ "field": "RECENCY" }],"first": 100,"after": "上一次返回的endCursor字符串"}}
注意事项
- 请参考Upwork官方文档确认
first参数的最大允许值,避免因超出限制导致请求失败 - 分页请求需控制频率,遵守API的速率限制规则,防止被限流
内容的提问来源于stack exchange,提问作者Bilal
相关产品推荐
相关产品推荐

