REST:Glassdoor API 需要 User-Agent header

REST: Glassdoor API requires User-Agent in header

这与 this question. I was trying to query the Glassdoor public API using the parameters 记录相关,但一直收到 403 Forbidden 响应。为确保查询参数被用于正确创建 URL,我采用了组合查询 URL 并在我的浏览器中进行了尝试,结果成功了。

从我的浏览器进行的查询向后工作,我设法弄清楚用户代理不仅需要作为 URL 中的参数,还需要在 [= 中传递25=].

所以将所有这些放在一起,这里是将成功查询 Glassdoor public API 的代码:

import urllib.request as request
import requests
import json
from collections import OrderedDict

# authentication information & other request parameters
params_gd = OrderedDict({
    "v": "1",
    "format": "json",
    "t.p": "xxxxxx",
    "t.k": "yyyyyyyy",
    "action": "employers",
    "employerID": "11111",
    # programmatically get the IP of the machine
    "userip": json.loads(request.urlopen("http://ip.jsontest.com/").read().decode('utf-8'))['ip'],
    "useragent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
})

# construct the URL from parameters
basepath_gd = 'http://api.glassdoor.com/api/api.htm'

# request the API
response_gd = requests.get(basepath_gd,
                           params=params_gd,
                           headers={
                               "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
                           })
# check the response code (should be 200)  & the content
response_gd
response_gd.content

我的问题是 - 为什么 User-Agent 需要在查询 header 中指定,而它已经是 URL 参数的一部分?如果没有用户代理 header,查询不应该工作吗?

fg,

一些供应商不喜欢将数据提供给自动化工具,这些工具可能只是在抓取他们的数据...他们 "can tell" 提供 "person" 而不是一些的方式之一有点古怪的 Python 脚本是通过检查浏览器通常应用的 User-Agent header。

在这个具体实例中,Glassdoor 发布了他们的 API Terms here,并且从第三页的顶部开始,他们声明 "We reserve the right to limit or block applications that make a large number of calls to the Glassdoor API that are not primarily in response to the direct actions of individual end users."

我倾向于认为这是通过查找 Header: User-Agent 来强制执行的,但大多数公司不会明确说明他们如何执行此操作。他们还要求您在显示其数据的已批准 webpage/site 主页上显示他们的徽标和 link。

希望对您有所帮助。