首先一些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']
