Tag = response.xpath("//script[contains(.,'" + SplitString + "')]").extract()
Tag = Tag[0].split(SplitString)
Tag = Tag[1].split("\"]")
Tag = codecs.getdecoder('unicode_escape')(Tag[0])[0]
<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>
你可以使用
tag.encode('utf-8')
对字符串进行正确的编码,这将返回字节,这意味着在写入文件时必须使用
wb
,例如
f = open('filename, 'wb')
。下面的脚本现在对字符串进行了编码。
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]