首先一些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. 关于jdbc收集

    一.如果我这样获得一个resultset ResultSet rs=statment.execquery("select * from tab"我如何能够从resultset中获得 ...

  2. mysql优化SQL语句的一般步骤及常用方法

    一.优化SQL语句的一般步骤 1. 通过show status命令了解各种SQL的执行频率 mysqladmin extended-status 或: show [session|global]sta ...

  3. c#枚举自定义,用于数据绑定。 z

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Enum)] public ...

  4. Web服务器与Servlet容器

    今日要闻: Oracle启动了JRE7到JRE8的自动更新, JRE8发布于2014.3,于2014.10成为java.com默认版本, JRE7发布于2011.7, Oracle指定的Java生命政 ...

  5. 【原创】用JAVA实现大文件上传及显示进度信息

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...

  6. 使用gdb调试多线程程序总结

    转:使用gdb调试多线程程序总结 一直对GDB多线程调试接触不多,最近因为工作有了一些接触,简单作点记录吧. 先介绍一下GDB多线程调试的基本命令. info threads 显示当前可调试的所有线程 ...

  7. 《Java数据结构与算法》笔记-CH5-链表-3双端链表

    /** * 双端链表的实现 */ class LinkA { public long dData; public LinkA next; public LinkA(long d) { dData = ...

  8. ZOJ-3201 Tree of Tree 树形DP

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3201 题意:给一颗树,每个节点有一个权值,求节点数为n的最大权子 ...

  9. Mono 之 Jexus

    Jexus web server for linux 是运行在Linux上的Web服务器.其安装和部署及其简单,直接支持Asp.net . 下载Jexus wget http://linux.j66. ...

  10. centos php php-fpm install

    好记性不如烂笔头,把自己安装的步骤记录下来 1.下载php-5.2.8以及php-5.2.8-fpm-0.5.10.diff.gz,放到/usr/local/src目录 2.解压php-5.2.8到/ ...