我們在抓取一些網站的時候可能會抓到一些\u003C這樣的數據,這些代碼是UNICODE編碼值。可用js 的 unescape() 函數來解碼。如:
document.write(unescape("\u003c\u003e")); // 輸出:<>
import execjs with open('js.js', encoding='utf8') as f: js = f.read() js = execjs.compile(js) str = "str = '"\u003Cp\u003E巴西近日發生一起駭人奪嬰案,一名23歲孕婦遭剖腹取嬰慘死,而\"子宮大盜\"竟是13歲的親妹妹。\u003C\u002Fp\u003E\u003Cimg src=\"http:\u002F\u002Fp1.pstatp.com\u002Flarge\u002Fpgc-image\u002FRgNfa7YDBeZOnd\" img_width=\"480\" img_height=\"319\" alt=\"巴西孕婦遭剖腹取嬰慘死 兒子溺亡 凶手 ret = js.call('f', str) with open('2.html', 'w', encoding='utf8') as f: f.write(ret) print(ret)
js.js
var f = function (str) { return unescape(str) }