亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

網(wǎng)頁(yè)爬蟲(chóng) - 關(guān)于Python的編碼與解碼問(wèn)題
高洛峰
高洛峰 2017-04-18 10:31:42
0
4
664

在python2中用urllib模塊去請(qǐng)求淘寶的IP地址查詢接口,返回的是一段json字符串,如下所示:

import urllib


def get_data(ip):
    url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip
    data = urllib.urlopen(url).read()

    return data


if __name__ == "__main__":
    result = get_data("59.151.5.5")
    print(result)

返回結(jié)果如下:

{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u5317","area_id":"100000","region":"\u5317\u4eac\u5e02","region_id":"110000","city":"\u5317\u4eac\u5e02","city_id":"110100","county":"","county_id":"-1","isp":"\u4e16\u7eaa\u4e92\u8054","isp_id":"100021","ip":"59.151.5.5"}}

在返回結(jié)果中,中文是以 unicode字符串表示,這樣不方便閱讀,我想讓結(jié)果中中文部分直接用中文表示,就像下面這樣:

"city":"北京","ISP":"中國(guó)電信"

如果是python3的話返回又是這樣的:


    b'{"code":0,"data":{"country":"\\u4e2d\\u56fd","country_id":"CN","area":"\\u534e\\u5317","area_id":"100000","region":"\\u5317\\u4eac\\u5e02","region_id":"110000","city":"\\u5317\\u4eac\\u5e02","city_id":"110100","county":"","county_id":"-1","isp":"\\u4e16\\u7eaa\\u4e92\\u8054","isp_id":"100021","ip":"59.151.5.5"}}'

請(qǐng)問(wèn)在 python2和python3中分別該如何轉(zhuǎn)碼呢?

高洛峰
高洛峰

擁有18年軟件開(kāi)發(fā)和IT教學(xué)經(jīng)驗(yàn)。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項(xiàng)目經(jīng)理、高級(jí)軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...

reply all(4)
伊謝爾倫

There are two methods in Python3 that can solve your problem:

  • print() function

Python3 starts with encoding defined as UTF-8, so you know, just print it directly:

>>> print("\u5317\u4eac\u5e02")
北京市
  • Utilize Unicode database

There is a built-in libraryunicodedata, you can call two methods in this library, as follows:

>>> import unicodedata as u

# 這段字符串是來(lái)自你給提供的內(nèi)容
>>> s = "\u5317\u4eac\u5e02"
>>> s1 = ''
>>> for i in s:
        s1 += u.lookup(u.name(i))

# 輸出結(jié)果    
>>> s1
'北京市'

To add, if you process characters individually, you can use the above method, but after answering just now, I discovered that your return value is a byte object. This kind of object processing is very simple in Python3. Modify your code as follows:

import urllib.request as r


def get_data(ip):
    url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip
    data = r.urlopen(url).read()

    return data


if __name__ == "__main__":
    result = get_data("59.151.5.5")
    print(eval(result))

The return value after my test is:

{'data': {'area_id': '100000', 'isp': '世紀(jì)互聯(lián)', 'country_id': 'CN', 'country': '中國(guó)', 'region_id': '110000', 'county_id': '-1', 'ip': '59.151.5.5', 'city': '北京市', 'area': '華北', 'county': '', 'city_id': '110100', 'isp_id': '100021', 'region': '北京市'}, 'code': 0}

Hope to adopt

Peter_Zhu

In Python 3, you can convert bytes to str through the decode method:

    result = get_data("59.151.5.5").decode('raw_unicode_escape')
PHPzhong

That’s good==

import json
print json.dumps(json.loads(result), ensure_ascii=False)
阿神

Please use py3

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template