首先一些Python/ target=_blank class=infotextkey>Python字符串處理的簡易常用的用法。
1.去掉重復空格
s = "hello hello hello"
s = ' '.join(s.split())
2.去掉所有回車(或其他字符或字符串)
s = "hellonhellonhello hellon"
print(s)
s = s.replace("n","")
print(s)
3.查找字符串首次出現的位置(沒有返回-1)
s = "hellonhellonhello hellon"
print(s.find('n'))
print(s.find('la'))
4.查找字符串從后往前找首次出現的位置(沒有返回-1)
s = "hellonhellonhello hellon"
print(s.rfind('n'))
print(s.rfind('la'))
5.將字符串轉化成列表list
s = "hellonhellonhello hellon"
print(list(s))
6.查找所有匹配的子串
import re
s = "hellonhellonhello hellon"
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.讀取一個文件的所有內容存到一個字符串里
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的內容了
5.多個關鍵字切分字符串
import re
re.split('; |, ',str)
>>> a='Beautiful, is; better*thannugly'
>>> import re
>>> re.split('; |, |*|n',a)
['Beautiful', 'is', 'better', 'than', 'ugly'