12

I am using wget to download a static html page. The W3C Validator tells me the page is encoded in UTF-8. Yet when I cat the file after download, I get a bunch of binary nonsense. I'm on Ubuntu, and I thought the default encoding was UTF-8? That's what my locale file seems to say. Why is this happening and how can I correct it?

Also, looks like Content-Encoding: gzip. Perhaps this makes a diff?

This is the simple request:

wget https://www.example.com/page.html

I also tried this:

wget https://www.example.com/page.html -q -O - | iconv -f utf-16 -t utf-8 > output.html

Which returned: iconv: illegal input sequence at position 40

cat'ing the file returns binary that looks like this:

l�?חu�`�q"�:)s��dġ__��~i��6n)T�$H�#���QJ

Result of xxd output.html | head -20 :

00000000: 1f8b 0800 0000 0000 0003 bd56 518f db44  ...........VQ..D
00000010: 107e a6bf 62d4 8a1e 48b9 d8be 4268 9303  .~..b...H...Bh..
00000020: 8956 082a 155e 7a02 21dd cbd8 3bb6 97ae  .V.*.^z.!...;...
00000030: 77cd ee38 39f7 a1bf 9d19 3bb9 0bbd 9c40  w..89.....;....@
00000040: 2088 12c5 de9d 9df9 be99 6f67 f751 9699   .........og.Q..
00000050: 500d 1d79 5eee a265 faec 7151 e4ab 6205  P..y^..e..qQ..b.
00000060: 4dd3 0014 1790 e7d0 77c0 ef2f cbf8 cde3  M.......w../....
00000070: cf1f 7d6c 7d69 ec16 d0d9 c67f 7d7d 56c9  ..}l}i......}}V.
00000080: 04c5 eb33 35fc e49e 2563 e908 ca10 0d45  ...35...%c.....E
00000090: 31ce afcf a022 e77a 34c6 fa46 46be d88f  1....".z4..FF...
000000a0: a41e ab79 446d 76d6 702b cf45 9e7f ba77  ...yDmv.p+.E...w
000000b0: 7dc2 779c 274e cc18 483c 3a12 0f75 f07c  }.w.'N..H<:..u.|
000000c0: 5e63 67dd b886 ab48 e550 b5c4 f0e3 db0d  ^cg....H.P......
000000d0: 54c1 85b8 8627 2ff3 2ff3 17f9 0626 d31d  T....'/./....&..
000000e0: d9a6 e5b5 4076 663f 94ec 7b5a 17cf 7ade  ....@vf?..{Z..z.
000000f0: 00d3 0d9f 4fcc d733 ef8d a0bb 0a06 c7eb  ....O..3........
00000100: b304 6fb1 b1cc 18ed 90e0 8710 43aa 424f  ..o.........C.BO
00000110: 50c7 d0c1 2bac 09be 4d1c 2566 335e 666c  P...+...M.%f3^fl
00000120: 1e20 951d 58fd 6774 f3e9 f317 749f 7fc4  . ..X.gt....t...
00000130: d651 cdca f5a7 b0a5 aea4 08ab 055c e4c5  .Q...........\..

Also, strangely, the output file seems to open properly in TextWrangler!

  • 1
    revising the question to provide the command being used and the output being generated would be huge help here. – steve Nov 6 '16 at 20:46
  • LANG=en_US.UTF-8 iconv: illegal input sequence at position 40 – ΓΓIICK Nov 6 '16 at 20:47
  • 1
    Can you show us what the "binary nonsense" looks like? I'd suggest running: xxd dubious.file | head -20` and including that as an update. – Bruce Ediger Nov 6 '16 at 20:47
  • @BruceEdiger updated. – ΓΓIICK Nov 6 '16 at 20:51
  • Also, strangely, the output file seems to open correctly in TextWrangler! – ΓΓIICK Nov 6 '16 at 21:06
26

This is a gzip compressed file. You can find this out by running the file command, which figures out the file format from magic numbers in the data (this is how programs such as Text Wrangler figure out that the file is compressed as well):

file output.html
wget -O - … | file -

The server (I guessed it from the content you showed) is sending gzipped data and correctly setting the header

Content-Encoding: gzip

but wget doesn't support that. In recent versions, wget sends Accept-encoding: identity, to tell the server not to compress or otherwise encode the data. In older versions, you can send the header manually:

wget --header 'Accept-encoding: identity' …

However this particular server appears to be broken: it sends compressed data even when told not to encode the data in any way. So you'll have to decompress the data manually.

wget -O output.html.gz … && gunzip output.html.gz
  • Very nice analysis – roaima Nov 7 '16 at 0:18
  • BTW, you can pipe into gunzip the same way as you do to file, if you don't want to store a temporary. You might want to pass -c or --to-stdout (or call it as zcat) to insert it into the pipeline in the question, between wget and iconv. – Toby Speight Nov 7 '16 at 16:28
15

This file is still compressed with gzip.

You can see that the first two bytes 0x1f8b match the gzip signature. So to read the data you need to uncompress it.

mv file.dat file.dat.gz
gunzip file.dat.gz

Or

zcat file.dat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.