前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用BeautifulSoup库抓取信息时去掉字符串首尾空白的几种方法

用BeautifulSoup库抓取信息时去掉字符串首尾空白的几种方法

作者头像
木制robot
发布2018-04-13 13:47:06
1.5K0
发布2018-04-13 13:47:06
举报

前言

在抓取网页信息时经常遇到很多头尾加了空格的字符串,在此介绍几种处理的小技巧。

例子

1.

代码语言:javascript
复制
<p>     woodenrobot       </p>

2.

代码语言:javascript
复制
<p>   woodenrobot1<em>  woodenrobot2  </em>  </p>

方法

对于例1

如果遇到例1这种情况下面几种方法可以通用。

代码语言:javascript
复制
from bs4 import BeautifulSoup
html = '<p>     woodenrobot       </p>'
soup = BeautifulSoup(html)
a = soup.get_text()
b = soup.get_text().strip()
c = soup.get_text(strip=True)
d = soup.strings
e = soup.stripped_strings
print('a: %s\nb: %s\nc: %s\nd: %s\ne: %s' % (a, b, c, list(d), list(e)))

输出结果如下:

代码语言:javascript
复制
a:    woodenrobot  
b: woodenrobot
c: woodenrobot
d: ['   woodenrobot  ']
e: ['woodenrobot']

其中a与d未处理去掉首尾空格,d, e是遍历整个子孙节点得到一个生成器。

对于例2

代码语言:javascript
复制
from bs4 import BeautifulSoup
html = '<p>   woodenrobot1<em>   woodenrobot2  </em>  </p>'
soup = BeautifulSoup(html)
a = soup.get_text()
b = soup.get_text().strip()
c = soup.get_text(strip=True)
d = soup.strings
e = soup.stripped_strings
print('a: %s\nb: %s\nc: %s\nd: %s\ne: %s' % (a, b, c, list(d), list(e)))

输出结果:

代码语言:javascript
复制
a:    woodenrobot1  woodenrobot2   
b: woodenrobot1  woodenrobot2
c: woodenrobot1woodenrobot2
d: ['   woodenrobot1', '  woodenrobot2  ', ' ']
e: ['woodenrobot1', 'woodenrobot2']

通过结果我们知道对于复杂一点的特殊结构这个三种方法还是有一些差异存在,所以我们需要根据不同的需求选择不同的方法。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-09-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 例子
    • 1.
      • 2.
      • 方法
        • 对于例1
          • 对于例2
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档