python读取api接口json数据
[python]
#by 侯禹江</span>
<span class="token comment">#2018、/0/27</span>
<span class="token keyword">import</span> json
<span class="token keyword">from</span> urllib<span class="token punctuation">.</span>request <span class="token keyword">import</span> Request<span class="token punctuation">,</span> urlopen
<span class="token comment">#zb网站获取数据Api</span>
url <span class="token operator">=</span> <span class="token string">"https://trans.bitkk.com/line/topall?area=&jsoncallback=jQuery191025699015513536727_1530079609291&_=1530079609293"</span>
<span class="token comment">#包装头部</span>
firefox_headers <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'User-Agent'</span><span class="token punctuation">:</span> <span class="token string">'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'</span><span class="token punctuation">}</span>
<span class="token comment">#构建请求</span>
request <span class="token operator">=</span> Request<span class="token punctuation">(</span> url<span class="token punctuation">,</span> headers<span class="token operator">=</span>firefox_headers <span class="token punctuation">)</span>
html <span class="token operator">=</span> urlopen<span class="token punctuation">(</span> request <span class="token punctuation">)</span>
<span class="token comment">#获取数据</span>
data <span class="token operator">=</span> html<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">#转换成字符串</span>
strs <span class="token operator">=</span> <span class="token builtin">str</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>
<span class="token comment">#截取字符串</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>strs<span class="token punctuation">)</span><span class="token punctuation">)</span>
strs_for_json <span class="token operator">=</span> strs<span class="token punctuation">[</span><span class="token number">44</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
strs_for_json<span class="token operator">=</span> strs_for_json<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>strs_for_json<span class="token punctuation">)</span>
<span class="token comment">#转换成JSON</span>
data <span class="token operator">=</span> strs_for_json
datas <span class="token operator">=</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
<span class="token comment">#转换成字典数据</span>
data_json <span class="token operator">=</span> json<span class="token punctuation">.</span>loads<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>data_json<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#<class 'dict'></span>
<span class="token keyword">print</span><span class="token punctuation">(</span>data_json<span class="token punctuation">[</span><span class="token string">'datas'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'market'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>data_json<span class="token punctuation">[</span><span class="token string">'datas'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'sell1Price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>data_json<span class="token punctuation">[</span><span class="token string">'datas'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token builtin">len</span> <span class="token operator">=</span> <span class="token builtin">len</span><span class="token punctuation">(</span>data_json<span class="token punctuation">[</span><span class="token string">'datas'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">#输出价格表</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"*****************************zb价格获取***************************************"</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token builtin">len</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"币种\市场类型:"</span><span class="token operator">+</span>data_json<span class="token punctuation">[</span><span class="token string">'datas'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'market'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">"^^^^^^^"</span><span class="token punctuation">,</span><span class="token string">"实时价格:"</span><span class="token operator">+</span>data_json<span class="token punctuation">[</span><span class="token string">'datas'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'sell1Price'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"^^^^^^^"</span><span class="token punctuation">,</span><span class="token string">"24小时最高价格:"</span><span class="token operator">+</span>data_json<span class="token punctuation">[</span><span class="token string">'datas'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'hightPrice'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"^^^^^^^"</span><span class="token punctuation">,</span><span class="token string">"24小时最低价格:"</span><span class="token operator">+</span>data_json<span class="token punctuation">[</span><span class="token string">'datas'</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'lastPrice'</span><span class="token punctuation">]</span><span class="token punctuation">)
</span>[/python]