Python常用网页字符串处理技巧
首先一些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常用网页字符串处理技巧的更多相关文章
- python常用的字符串格式化有哪几种?
常用字符串格式化%和format 皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为 ...
- python 常用的字符串方法
st = ' hello Kitty 'str = 'hello {name} {age}' #print(st.format(name='fadfa'))#常用的字符串方法print(st.coun ...
- [Python Study Notes]字符串处理技巧(持续更新)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- python常用连接字符串
1.使用占位符% print(('%s%s%s' % ('one','two', 'three'))) 2.'+'号连接 字符串是不可变对象,每次改变会申请一块新的内存,操作符+连接字符串的时候会涉及 ...
- C++常用的字符串处理函数-全
这是自己用stl实现的一些字符串处理函数和常用的字符串处理技巧,经验正基本无误,可直接使用,若有问题,可相应列出 包括:split string to int int to string join # ...
- [python] 常用正则表达式爬取网页信息及分析HTML标签总结【转】
[python] 常用正则表达式爬取网页信息及分析HTML标签总结 转http://blog.csdn.net/Eastmount/article/details/51082253 标签: pytho ...
- Python 编程语言要掌握的技能之一:使用数字与字符串的技巧
最佳实践 1. 少写数字字面量 “数字字面量(integer literal)” 是指那些直接出现在代码里的数字.它们分布在代码里的各个角落,比如代码 del users[0] 里的 0 就是一个数字 ...
- Python使用数字与字符串的技巧
1.少写数字字面量 "数字字面量(integer literal)" 是指那些直接出现在代码里的数字.它们分布在代码里的各个角落,比如代码 del users[0] 里的 0 就是 ...
- Python 工匠:使用数字与字符串的技巧
序言 这是 "Python 工匠"系列的第 3 篇文章. 数字是几乎所有编程语言里最基本的数据类型,它是我们通过代码连接现实世界的基础.在 Python 里有三种数值类型:整型(i ...
随机推荐
- 计算机网络——超文本传送协议HTTP
一.简述 每个万维网网点都有一个服务器进程,它不断地监听TCP的端口80,以便发现是否有浏览器向它发出连接建立请求.一旦监听到连接建立请求并建立了TCP连接之后,浏览器就向万维网服务器发出浏览某个页面 ...
- Linux makefile教程之更新函数库文件十[转]
使用make更新函数库文件 ——————————— 函数库文件也就是对Object文件(程序编译的中间文件)的打包文件.在Unix下,一般是由命令"ar"来完成打包工作. 一.函数 ...
- JDK 1.6 下载 地址
JDK1.6官方下载_JDK6官方下载地址: http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin ...
- Files
write public static void write(CharSequence from, File to, Charset charset) throws IOException { asC ...
- 【转】linux之mkfs/mke2fs格式化
转自:http://blog.csdn.net/andyhooo/article/details/5321584 mkfs [root@www ~]# mkfs [-t 檔案系統格式] 裝置檔名 選項 ...
- linux中清除cache的方法
在进行测试文件导入的时候,发现内存占用很大,如下所示: [root@python ~]# vmstat 1 -S M 3 procs -----------memory---------- ---sw ...
- The serializable class does not declare a static final serialVersionUID field of type long
在编译以下Java程序时,出现The serializable class does not declare a static final serialVersionUID field of typ ...
- WCF_Config頁面常用配置
右键点击App.config文件,选中Edit WCF Configuration进行编辑,我们添加2个baseAddress,一个是基于HTTP协议的:一个是基于TCP协议的.同时添加2个bindi ...
- AI教程
AI制作铅笔图案笔刷 在下面的步骤中,我们将学习如何在Adobe Illustrator中创建铅笔笔刷.首先你要建立一个简单的网格,以及如何使用基本的工具和形状创建主要的形状.然后,学习如何保 ...
- Windows Azure 配置多个站点的虚拟网络连接
通过上一篇"Windows Azure 虚拟网络配置(Site to Site)" 我们建立了可以进行Site to Site连接的虚拟网络,配置过后有些朋友会有疑问:如果需要连接 ...