Unescape Hex代码点不用改变Python中的编码

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (106)

在抓取时,我在提取的数据中得到十六进制代码点,如\ u0026#39; 和\ u003c。问题是在提取时,他们会在他们之前得到一个“\”,例如\\ u0026#39和\\ u003c。所以要解决我用过的问题,

Tag = response.xpath("//script[contains(.,'" + SplitString + "')]").extract()
Tag = Tag[0].split(SplitString)
Tag = Tag[1].split("\"]")
Tag = codecs.getdecoder('unicode_escape')(Tag[0])[0]

但使用“unicode_escape”的问题是,它改变了一些特殊符号像🎈❤️🎈❤️🎈并将其转换成°ÂŸÂŽÂâ¤ï¸ÂðŸŽÂâ¤ï¸Â🎠。那么我该如何解决这个问题呢?

源代码中的脚本如下:

<script nonce="q0OGvOrA73kvqp+Tk1lGIR+glJc">AF_initDataCallback({key: 'ds:4', isError:  false , hash: '17', data:function(){return [[["Machineship"]
,null,null,[1]
,null,[["CBSqARUKEwiZjNDE8bDcAhWKI2gKHV8ZDtA\u003d"]
,["CBSqARUKEwiYjNDE8bDcAhWKI2gKHV8ZDtA\u003d"]],[[null,"Enjoy different kind of magazines and entertainment\u003cbr\u003e3 on the various supported classic rule.\u003cbr\u003e\u003cbr\u003e[Feature]\u003cbr\u003e- 1 to max 4 can join\u003cbr\u003e- You can select one of three different patterns.\u003cbr\u003e- :\u003cbr\u003ehttps://www.example.com"]
,[null,"Best \u0026quot;ad-free\u0026quot; entertainment for kids!\u003cbr\u003e🎈❤️🎈❤️🎈"]
]
,null,[[[null,2,[800,1200]............... </script>

从此我想提取:为孩子们提供最好的“无广告”娱乐! 🎈❤️🎈❤️🎈

提问于
用户回答回答于

我试图创建一个脚本来复制你的错误。这是我到目前为止:

from scrapy.selector import Selector

body = '<script nonce="q0OGvOrA73kvqp+Tk1lGIR+glJc">AF_initDataCallback({key: \'ds:4\', isError:  false , hash: \'17\', data:function(){return [[["Machineship"]\n,null,null,[1]\n,null,[["CBSqARUKEwiZjNDE8bDcAhWKI2gKHV8ZDtA\u003d"]\n,["CBSqARUKEwiYjNDE8bDcAhWKI2gKHV8ZDtA\u003d"]],[[null,"Enjoy different kind of magazines and entertainment\u003cbr\u003e3 on the various supported classic rule.\u003cbr\u003e\u003cbr\u003e[Feature]\u003cbr\u003e- 1 to max 4 can join\u003cbr\u003e- You can select one of three different patterns.\u003cbr\u003e- :\u003cbr\u003ehttps://www.example.com"]\n,[null,"Best \u0026quot;ad-free\u0026quot; entertainment for kids!\u003cbr\u003e🎈❤️🎈❤️🎈"]\n]\n,null,[[[null,2,[800,1200]............... </script>'

split_string = 'm"]\n,[null,"'

tag = Selector(text=body).xpath("//script[contains(.,'" + split_string + "')]").extract()

tag = tag[0].split(split_string)
tag = tag[1].split("\"]")
tag = tag[0]

print(tag)

它打印: Best &quot;ad-free&quot; entertainment for kids!<br>🎈❤️🎈❤️🎈

扫码关注云+社区

领取腾讯云代金券