首先一些Python字符串处理的简易常用的用法。其他的以后用到再补充。

1.去掉重复空格

s = "hello   hello   hello"
s = ' '.join(s.split())

2.去掉所有回车(或其他字符或字符串)

s = "hello\nhello\nhello hello\n"
print(s)
s = s.replace("\n","")
print(s)

3.查找字符串首次出现的位置(没有返回-1)

s = "hello\nhello\nhello hello\n"
print(s.find('\n'))
print(s.find('la'))

4.查找字符串从后往前找首次出现的位置(没有返回-1)

s = "hello\nhello\nhello hello\n"
print(s.rfind('\n'))
print(s.rfind('la'))

5.将字符串转化成列表list

s = "hello\nhello\nhello hello\n"
print(list(s))

6.查找所有匹配的子串

import re

s = "hello\nhello\nhello hello\n"
print(re.findall('hello',s)) # hello也可以换成正则表达式

然后是网页字符串处理的高端用法:(综合运用requests模块,beautifulsoup模块,re模块等)

1.requests获取一个链接的内容并原封不动写入文件

import requests

r = requests.get('https://baike.baidu.com')
with open('test.html', 'wb') as fd:
for chunk in r.iter_content(100):
fd.write(chunk)

2.读取一个文件的所有内容存到一个字符串里

# encoding : utf-8

with open('test.html','r',encoding='utf-8') as f:
content = f.readlines()
content = ''.join(content)
# content = content.replace('\n','') # 如果想去掉回车可以加上这行
print(content)

3.把网页字符串用BeautifulSoup存起来处理

from bs4 import BeautifulSoup

soup = BeautifulSoup(content,'html.parser')
print(soup.prettify())

4.存到BeautifulSoup里之后这个字符串就可以任你摆布了,比如:提取出所有<a>标签

soup = BeautifulSoup(content,'html.parser')
print(soup.find_all('a'))

或者提取出所有<a>标签和<b>标签

soup = BeautifulSoup(content,'html.parser')
print(soup.find_all(['a','b']))

这些属于beautifulsoup的内容了,可以看官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

也可以看我的另一篇博客:http://www.cnblogs.com/itlqs/p/5902678.html

5.多个关键字切分字符串

import re
re.split('; |, ',str) >>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']

Python常用网页字符串处理技巧的更多相关文章

  1. python常用的字符串格式化有哪几种?

    常用字符串格式化%和format 皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为 ...

  2. python 常用的字符串方法

    st = ' hello Kitty 'str = 'hello {name} {age}' #print(st.format(name='fadfa'))#常用的字符串方法print(st.coun ...

  3. [Python Study Notes]字符串处理技巧(持续更新)

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  4. python常用连接字符串

    1.使用占位符% print(('%s%s%s' % ('one','two', 'three'))) 2.'+'号连接 字符串是不可变对象,每次改变会申请一块新的内存,操作符+连接字符串的时候会涉及 ...

  5. C++常用的字符串处理函数-全

    这是自己用stl实现的一些字符串处理函数和常用的字符串处理技巧,经验正基本无误,可直接使用,若有问题,可相应列出 包括:split string to int int to string join # ...

  6. [python] 常用正则表达式爬取网页信息及分析HTML标签总结【转】

    [python] 常用正则表达式爬取网页信息及分析HTML标签总结 转http://blog.csdn.net/Eastmount/article/details/51082253 标签: pytho ...

  7. Python 编程语言要掌握的技能之一:使用数字与字符串的技巧

    最佳实践 1. 少写数字字面量 “数字字面量(integer literal)” 是指那些直接出现在代码里的数字.它们分布在代码里的各个角落,比如代码 del users[0] 里的 0 就是一个数字 ...

  8. Python使用数字与字符串的技巧

    1.少写数字字面量 "数字字面量(integer literal)" 是指那些直接出现在代码里的数字.它们分布在代码里的各个角落,比如代码 del users[0] 里的 0 就是 ...

  9. Python 工匠:使用数字与字符串的技巧

    序言 这是 "Python 工匠"系列的第 3 篇文章. 数字是几乎所有编程语言里最基本的数据类型,它是我们通过代码连接现实世界的基础.在 Python 里有三种数值类型:整型(i ...

随机推荐

  1. Android 从java字节码告诉你 为什么Handler会造成内存泄露

    很多人面试的时候,都知道Handler 极易造成内存泄露,但是有一些讲不出来为什么,好一点的 会告诉你looper msg 之类的,但是你再往下问 为什么msg持有handler handler为什么 ...

  2. css实现鼠标经过导航文字偏位效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. myeclipse安装

    http://www.cnol.cc/2013/1427.html(我用这个) http://www.myeclipseide.cn/windows.html http://download.csdn ...

  4. linux 下按在sqllite

    1 安装 去sqlite主页http://www.sqlite.org/.跳转到下载也http://www.sqlite.org/download.html.源码下载sqlite-amalgamati ...

  5. C#的Timer

    PowerCoder 原文 C#的Timer 再C#里现在有3个Timer类: System.Windows.Forms.Timer System.Threading.Timer System.Tim ...

  6. T-SQL:SQL Server-数据开发(经典)

    ylbtech-SQL Server-Doc-Help:SQL Server-数据开发(经典) SQL Server 数据开发(经典). 1,数据开发(经典) 返回顶部 1.按姓氏笔画排序: Sele ...

  7. 解决:cc1.exe: sorry, unimplemented: 64-bit mode not compiled in

    在win下用Go语言的cgo时(比如下面场景)我们会用到的GCC编译器,Win下我们一般用MinGW. Golang连接Oracle数据库:win下 golang 跨平台编译 MinGW全称Minim ...

  8. python+selenium环境搭建

    这里主要基于windows平台. 下载python.http://python.org/getit/ 下载setuptools [python的基础包工具].http://pypi.python.or ...

  9. 【LeetCode】111 - Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  10. BITED数学建模七日谈之五:怎样问数学模型问题

    下面进入数学建模经验谈第五天:怎样问数学模型问题 写这一篇的目的主要在于帮助大家能更快地发现问题和解决问题,让自己的模型思路有一个比较好的形成过程. 在我们学习数学模型.准备比赛的时候,经常会遇到各种 ...