我写了一个bash脚本,使用curl从网站获取输出并在html输出上进行一系列的字符串操作。问题是当我对返回其输出为gzipped的网站运行它时,会出现问题。在浏览器中访问该网站没有问题。
当我手动运行curl时,我得到了gzipped输出:
$ curl "http://example.com"
以下是来自该网站的标题:
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/html; charset=utf-8
X-Powered-By: PHP/5.2.17
Last-Modified: Sat, 03 Dec 2011 00:07:57 GMT
ETag: "6c38e1154f32dbd9ba211db8ad189b27"
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: must-revalidate
Content-Encoding: gzip
Content-Length: 7796
Date: Sat, 03 Dec 2011 00:46:22 GMT
X-Varnish: 1509870407 1509810501
Age: 504
Via: 1.1 varnish
Connection: keep-alive
X-Cache-Svr: p2137050.pubip.peer1.net
X-Cache: HIT
X-Cache-Hits: 425
我知道返回的数据是经过gzip压缩的,因为这个返回的是HTML,正如预期的那样:
$ curl "http://example.com" | gunzip
我不想通过gunzip来管道输出,因为该脚本在其他站点上按原样工作,而通过gzip进行管道传输会破坏该功能。
我的尝试
- 更改用户代理(我尝试了与浏览器发送的相同字符串“Mozilla/4.0”等)
- man curl
- 谷歌搜索
- 在stackoverflow上搜索
所有的都没有结果
有什么想法吗?
curl 7.54.0(x86_64-apple-darwin17.0)libcurl/7.54.0 LibreSSL/2.0.20 zlib/1.2.11 nghttp2/1.24.0
)-通过从Accept-Encoding
中删除br
来解决它。请参见https://dev59.com/tmMk5IYBdhLWcg3wxAry。 - The Onincurl -sSv https://stackoverflow.com/ |&rg -i'gzip|accept'
,和--compressed
一起使用。除非curl
通过Accept-Encoding
,否则服务器不会对响应进行gzip压缩。 - x-yuri