python 爬虫爬取内容时, \xa0 、 \u3000 的含义与处理方法

本文介绍了在使用Python爬虫抓取网页内容时遇到的特殊空白字符xa0与u3000,并提供了处理这些字符的方法。xa0代表不间断空白符,在latin1字符集中定义;u3000则是一种全角空白符,属于CJK标点符号区块。文章给出了具体的Python代码来清理这些特殊空白字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python 爬虫爬取内容时, \xa0 、 \u3000 的含义

转自:https://www.cnblogs.com/BlackStorm/p/6359005.html

处理方法 str.replace(u'\xa0', u' ')

最近用 scrapy 爬某网站,发现拿到的内容里面含有 \xa0 、 \u3000 这样的字符,起初还以为是编码不对,搜了一下才知道是见识太少 233 。

\xa0 是不间断空白符  

我们通常所用的空格是 \x20 ,是在标准ASCII可见字符 0x20~0x7e 范围内。
而 \xa0 属于 latin1 (ISO/IEC_8859-1)中的扩展字符集字符,代表空白符nbsp(non-breaking space)
latin1 字符集向下兼容 ASCII ( 0x20~0x7e )。通常我们见到的字符多数是 latin1 的,比如在 MySQL 数据库中。

这里也有一张简陋的Latin1字符集对照表

\u3000 是全角的空白符

根据Unicode编码标准及其基本多语言面的定义, \u3000 属于CJK字符CJK标点符号区块内,是空白字符之一。它的名字是 Ideographic Space ,有人译作表意字空格、象形字空格等。顾名思义,就是全角的 CJK 空格。它跟 nbsp 不一样,是可以被换行间断的。常用于制造缩进, wiki 还说用于抬头,但没见过。

这里还有一个 Unicode.org 上关于 CJK 标点符号块的字符代码表

转自https://www.cnblogs.com/my8100/p/7709371.html

HTML转义字符&npsp;表示non-breaking space,unicode编码为u'\xa0',超出gbk编码范围?

0.目录

1.参考
2.问题定位
不间断空格的unicode表示为 u\xa0',超出gbk编码范围?
3.如何处理
.extract_first().replace(u'\xa0', u' ').strip().encode('utf-8','replace')

 

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乱码解决 

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

 

2.问题定位

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

定位元素显示为 &npsp;


### 使用Python实现微博帖子爬虫方法 为了使用 Python 实现微博帖子的爬取,可以采用 `Requests` 和 `Pandas` 库来完成这一过程[^1]。下面是一个简单的例子展示如何构建一个基本的微博爬虫。 #### 安装必要的库 首先安装所需的第三方库: ```bash pip install requests pandas beautifulsoup4 ``` #### 导入所需模块并设置请求头 导入必要的 Python 模块,并配置好模拟浏览器访问所必需的头部信息: ```python import requests from bs4 import BeautifulSoup import pandas as pd headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' } ``` #### 获取网页内容 定义函数用于发送 HTTP 请求获取页面 HTML 文本: ```python def get_page(url): try: response = requests.get(url, headers=headers) if response.status_code == 200: return response.text else: print(f"Failed to load page {url}") return None except Exception as e: print(e) return None ``` #### 解析HTML文档提取数据 编写解析器从返回的数据中抽取有用的信息,比如用户名、发布间以及具体内容等: ```python def parse_html(html): soup = BeautifulSoup(html, "html.parser") posts = [] items = soup.find_all('div', class_='c') # 假设每条微博都在此类名为'c'的<div>标签内 for item in items: post_info = {} author_tag = item.select_one('.ctt a') time_tag = item.select_one('.ct span') if not author_tag or not time_tag: continue post_info['author'] = author_tag.string.strip() post_info['time'] = time_tag.next_sibling.strip().replace(u'\xa0', u' ') post_info['content'] = ''.join([str(x).strip() for x in list(item.stripped_strings)[1:-2]]) posts.append(post_info) return posts ``` #### 存储抓取下来的数据 最后一步就是将收集好的数据保存成 CSV 文件或者其他格式方便后续处理: ```python dataframe = pd.DataFrame(posts) dataframe.to_csv("weibo_posts.csv", index=False, encoding='utf_8_sig') print("Data has been saved successfully.") ``` 需要注意的是,在实际操作过程中应当遵循网站的服务条款,尊重用户隐私权,确保合法合规地开展工作[^2]。此外,由于社交平台反爬机制的存在,建议适当调整请求频率以免触发封禁措施。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值