鍍金池/ 問(wèn)答/Python  C  C++  HTML/ 網(wǎng)站返回的字節(jié)里有16進(jìn)制,如何在python轉(zhuǎn)成字符串.

網(wǎng)站返回的字節(jié)里有16進(jìn)制,如何在python轉(zhuǎn)成字符串.

b'{"action":"auth","msg":"eval(\\"\\\\x4d\\\\x61\\\\x74\\\\x68\\\\x2e\\\\x63\\\\x6f\\\\x73\\\\x28\\\\x31\\\\x35\\\\x31\\\\x35\\\\x37\\\\x37\\\\x38\\\\x31\\\\x34\\\\x30\\\\x29\\")"}'

上面是網(wǎng)站返回的數(shù)據(jù), 不知道在python里怎么把上面轉(zhuǎn)為Math.cos(1515778140)
我能得到Math.cos(1515778140)這個(gè)結(jié)果的步驟是:

eval(\\"\\\\x4d\\\\x61\\\\x74\\\\x68\\\\x2e\\\\x63\\\\x6f\\\\x73\\\\x28\\\\x31\\\\x35\\\\x31\\\\x35\\\\x37\\\\x37\\\\x38\\\\x31\\\\x34\\\\x30\\\\x29\\")

手動(dòng)轉(zhuǎn)為

alert("\x4d\x61\x74\x68\x2e\x63\x6f\x73\x28\x31\x35\x31\x35\x37\x37\x38\x31\x34\x30\x29")

放到控制臺(tái)console執(zhí)行后得到的, 求問(wèn)如何在python得到Math.cos(1515778140)

回答
編輯回答
大濕胸
# -*- coding: utf-8 -*-

import json

msg = b'{"action":"auth","msg":"eval(\\"\\\\x4d\\\\x61\\\\x74\\\\x68\\\\x2e\\\\x63\\\\x6f\\\\x73\\\\x28\\\\x31\\\\x35\\\\x31\\\\x35\\\\x37\\\\x37\\\\x38\\\\x31\\\\x34\\\\x30\\\\x29\\")"}'
msg = json.loads(msg)['msg']

#復(fù)雜點(diǎn)的
import re
print ''.join(chr(int(s, 16)) for s in re.findall(r'\\x(.{2})', msg))

#簡(jiǎn)單點(diǎn)的
print eval(msg.replace('eval', ''))
2017年8月29日 22:32