如果需要在 curl 请求中设置用户头字符串,可以使用-H 选项设置用户代理,如:
curl -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" http://stackoverflow.com/questions/28760694/how-to-use-curl-to-get-a-get-request-exactly-same-as-using-chrome
在 2021 年 2 月 22 日从最新的 Chrome 中更新了用户代理
使用像Charles Proxy 这样的代理工具确实有助于缩短您所要求的工作。以下是我所做的,以这个 SO 页面为例(截至 2015 年 7 月,使用 Charles 版本 3.10):
- 让 Charles Proxy 运行
- 使用浏览器发出网络请求
- 在 Charles Proxy 中查找所需的请求
- 在 Charles Proxy 中右键单击请求
- 选择“复制 cURL 请求”
您现在有一个 cURL 请求,您可以在终端中运行该请求,该请求将反映您的浏览器发出的请求。这是我对该页面的请求的样子(删除了 cookie 标头):
curl -H "Host: stackoverflow.com" -H "Cache-Control: max-age=0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "HTTPS: 1" -H "DNT: 1" -H "Referer: https://www.google.com/" -H "Accept-Language: en-US,en;q=0.8,en-GB;q=0.6,es;q=0.4" -H "If-Modified-Since: Thu, 23 Jul 2015 20:31:28 GMT" --compressed http://stackoverflow.com/questions/28760694/how-to-use-curl-to-get-a-get-request-exactly-same-as-using-chrome