【问题标题】:Unescape Hex code point \u0026 without altering the encoding in PythonUnescape Hex 代码点 \u0026 而不改变 Python 中的编码
【发布时间】:2018-07-21 16:22:38
【问题描述】:

在抓取时,我在提取的数据中获得了十六进制代码点,例如 \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>

我想从中提取:儿童最佳“无广告”娱乐!
????❤️????❤️????

【问题讨论】:

  • SplitString 指的是什么?标签设置好后怎么处理?您能否提供一个简短的问题示例,我将尝试复制该问题以查看发生了什么。
  • SplitString 是字符组合,首先用于从网站的脚本标签中找到特定组合,然后将文本从该节点或特别是 SplitSrting = ("]\ n,[bullpup,)
  • 我写了一个快速刮板,从一个网站上的div 读取&amp;#39;????❤️????❤️????,我将它写入文件没有问题。我看不到您在哪里添加了\\u,但我认为您需要做的就是从字符串中的每次出现中删除一个斜杠。如果您将脚本添加到复制您的问题的问题中,我可能会看到发生了什么。
  • 已添加脚本的sn-p。
  • 澄清您将为该示例输入的 SplitString 内容是什么?

标签: python encoding web-scraping scrapy hex


【解决方案1】:

您可以使用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]

f = open('test.txt', 'wb')
tag = tag.encode('utf-8')
f.write(tag)
f.close()

print('done')

打印:Best &amp;quot;ad-free&amp;quot; entertainment for kids!&lt;br&gt;?❤️?❤️?

我不确定你从哪里得到双斜杠,但实际上可能需要它们来转义字符串中的斜杠,所以可能不会造成问题。

【讨论】:

  • 先生,但是当我这样做时,它显示为最佳\u0026quot;无广告\u0026quot;孩子们的娱乐!\u003cbr\u003e?❤️?❤️? 在终端中,同样导出到 csv。另外,必须注意的是,当我尝试在 sublime 中打印此类字符串时,它会自动更正它们以提供正确的结果。目前我正在使用替换作为 Tag.replace("\\u0026", "&").replace("\\u003c", "")。跨度>
  • 您使用的是哪个版本的 Python?
  • Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
  • 你有机会看看这个吗?我已经更新了我的答案,并认为它对你有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
相关资源
最近更新 更多