I want to import data from this website: http://hq.sinajs.cn/list=sh601006;

It should look like

var hq_str_sh601006= "大秦铁路,13.13,12.87,13.37,13.77,12.95,13.38,13.39,356310317,4776670723,1630,13.38,21200,13.37,62186,13.36,104760,13.35,152300,13.34,71700,13.39,55056,13.40,24100,13.41,39400,13.42,22366,13.43,2015-04-17,15:03:06,00";"

but when I input


the output is $Failed. How can I fix this?


It looks like Import has trouble with the character encoding on my computer:

Import["http://hq.sinajs.cn/list=sh601006", "HTML"]
(* "var hq_str_sh601006="`óÇØÌú·,13.13,12.87,13.37,13.77,12.95,13.38, [...] ;" *)

We can find the encoding by looking at the HTML headers:

Grid@URLFetch["http://hq.sinajs.cn/list=sh601006", "Headers"]
Cache-Control   no-cache
Content-Length  239
Connection      Keep-Alive
Content-Type    application/x-javascript; charset=GBK

"GBK" is not one of the possible $CharacterEncodings. We can try all possible encodings to see if any work:

   Import["http://hq.sinajs.cn/list=sh601006", "Text", 
    CharacterEncoding -> e]}, {e, $CharacterEncodings}]]

I find that "CP936" and "MacintoshChineseSimplified" work:

Import["http://hq.sinajs.cn/list=sh601006", "HTML", CharacterEncoding -> "CP936"]
(* var hq_str_sh601006="大秦铁路,13.13,12.87,13.37,13.77,12.95,13.38,13.39 [...] "; *)

I have found that

Import["http://hq.sinajs.cn/list=sh601006", "HTML"]


  • $\begingroup$ I have found that "Text" works well on unformatted data. $\endgroup$ – 2012rcampion Apr 19 '15 at 15:24

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