首页前端开发其他前端知识HTML转义字符&npsp;表示non-breaking space \xa0

HTML转义字符&npsp;表示non-breaking space \xa0

时间2024-02-10 17:51:02发布访客分类其他前端知识浏览805
导读:收集整理的这篇文章主要介绍了HTML转义字符&npsp;表示non-breaking space \xa0,觉得挺不错的,现在分享给大家,也给大家做个参考。 1.参考Beauti...
收集整理的这篇文章主要介绍了HTML转义字符& npsp;表示non-breaking space \xa0,觉得挺不错的,现在分享给大家,也给大家做个参考。

1.参考

Beautiful Soup and Unicode Problems

详细解释

unicodedata.normalize('NFKD',string) 实际作用???

Scrapy : Select tag wITh non-breaking space with xpath

>
    >
    >
     selector.xpath(u'''... //p[normalize-space()]... [not(contains(normalize-space(), "\u00a0"))]

normalize-space() 实际作用???

In [244]: sel.css('.content')
Out[244]: [Selector xpath=u"descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' content ')]" data=u'p class="content text-

BeautifulSoup下Unicode乱码解决

今天在用scrapy爬某个网站的数据,其中DOM解析我用的是BeautifulSoup,速度上没有XPath来得快,不过因为用了习惯了,所以一直用的bs,版本是bs4
不过在爬取过程中遇到了一些问题,其中一个是Unicode转码问题,这也算是python中一个著名问题了。
我遇到的算是BeautifulSoup中的一个奇葩bug吧,在网页中经常会有 & nbsp 这种标记,称为 non-breaking space character, 本来这个应该是忽略的,但在bs中会把这个符号
转义成为一个unicode编码 \xa0, 这就导致了后面如果要对内容处理的话会出现UnicodeError, 特别是如果使用的是Console或者scrapy中写文件、写数据库的piPEline操作时,
出现无法转义的错误。
那么该如何解决呢,其实不难

s = u'\xa0'
s.replace(u'\xa0', u'')

之后就可以对s进行encode,比如:

s = u'\xa0'
s.replace(u'\xa0', u'').encode('utf-8')

特别是在我的项目中,如果需要把数据写到MongoDB中,这个bug fix完后,写数据立刻搞定,爬取的内容全部写到MongoDB中。

s.replace(u'\xa0', u'').encode('utf-8')

2.问题定位

https://en.wikipedia.org/wiki/Comparison_of_text_editors

定位元素显示为 & npsp;

网页源代码表示为 & #160;

tr>
    td style="background: #FFD;
     color: black;
     vertical-align: middle;
     text-align: center;
    " class="partial table-partial">
    memory/td>
    td>
    = Limited by available memory &
    #160;
    &
    #160;
    /td>
    td style="background:#F99;
    vertical-align:middle;
    text-align:center;
    " class="table-no">
    No (64&
    #160;
    KB)/td>
    td>
    = Some limit less than available memory (give max size if known)/td>
    /tr>
    /table>
    

实际传输Hex为:

不间断空格的unicode表示为 u\xa0',保存的时候编码 utf-8 则是 '\xc2\xa0'

In [211]: for tr in response.xpath('//table[8]/tr[2]'):
...: PRint [u''.join(i.xpath('.//text()').extract()) for i in tr.xpath('./*')]
...:

[u'memory', u'= Limited by available memory \xa0\xa0', u'No (64\xa0KB)', u'= Some limit less than available memory (give max size if known)']

In [212]: u'No (64\xa0KB)'.encode('utf-8')
Out[212]: 'No (64\xc2\xa0KB)'

In [213]: u'No (64\xa0KB)'.encode('utf-8').decode('utf-8')
Out[213]: u'No (64\xa0KB)'

保存 csv 直接使用 excel 打开会有乱码(默认ANSI gbk 打开???,u'\xa0' 超出 gbk 能够编码范围???),使用记事本或notepad++能够自动以 utf-8 正常打开。

使用记事本打开csv文件,另存为 ANSI 编码,之后 excel 正常打开。超出 gbk 编码范围的替换为'?'

3.如何处理

.extract_First().replace(u'\xa0', u' ').strip().encode('utf-8','replace')

以上就是HTML转义字符& npsp;表示non-breaking space \xa0的详细内容,更多关于HTML转义字符\xa0的资料请关注其它相关文章!

您可能感兴趣的文章:
  • Python 字符串处理特殊空格\xc2\xa0\t\n Non-breaking space

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: HTML转义字符&npsp;表示non-breaking space \xa0
本文地址: https://pptw.com/jishu/608682.html
VSCode常用知识小结 详解Google Protobuf简明教程

游客 回复需填写必要信息