0 black 1 red 2 green 3 yellow 4 blue 5 magenta 6 cyan 7 white 9 default
例子
\033[43m 黃底 \033[41m 紅底 \033[43;31m 黃底紅字
進階版三(highlight 特定文字)
python
from bs4 import BeautifulSoup from termcolor import colored
with open("C:/Users/ouoholly/Desktop/data.html", encoding="utf-8") as f: soup = BeautifulSoup(f) classcolor: PURPLE = '\033[95m' BOLD = '\033[1m' END = '\033[0m' u = soup.find_all('div', class_="user") c = soup.find_all('div', class_="content") for (uu, cc) in zip(u, c): allu = uu.text # 當出現 ಠ_ಠ 和 pizza1234 時,highlight它們 searchusers=['ಠ_ಠ','pizza1234'] colortext = [] for t in allu.lower().split(): if t in searchusers: colortext.append(colored(t,'magenta','on_red')) else: colortext.append(t)
from bs4 import BeautifulSoup from termcolor import colored import re
with open("C:/Users/ouoholly/Desktop/data.html", encoding="utf-8") as f: soup = BeautifulSoup(f) u = soup.find_all('div', class_="user") c = soup.find_all('div', class_="content") for (uu, cc) in zip(u, c): # 只顯示 ಠ_ಠ 和 pizza1234 的發言 allusers = uu.text r = re.search('.*ಠ_ಠ.*|.*pizza1234.*', allusers) targetusers = [] usercontent = [] if r: targetusers.append(colored(uu.text,'magenta',attrs=['bold'])) usercontent.append(cc.text) for i in targetusers: if i==[]: list.remove(i) print(''.join(targetusers)) for i in usercontent: if i==[]: list.remove(i) print(''.join(usercontent) + '\n')
進階版五(看所有提及特定用戶的發言)
python
from bs4 import BeautifulSoup from termcolor import colored import re
with open("C:/Users/ouoholly/Desktop/data.html", encoding="utf-8") as f: soup = BeautifulSoup(f) u = soup.find_all('div', class_="user") c = soup.find_all('div', class_="content") for (uu, cc) in zip(u, c): # 顯示 ಠ_ಠ 和 pizza1234 的發言,也顯示其他人提及過 ಠ_ಠ 和 pizza1234 的發言 all = uu.text + cc.text r = re.search('.*ಠ_ಠ.*|.*pizza1234.*', all) targetusers = [] usercontent = [] if r: targetusers.append(colored(uu.text,'magenta',attrs=['bold'])) usercontent.append(cc.text) for i in targetusers: if i==[]: list.remove(i) print(''.join(targetusers)) for i in usercontent: if i==[]: list.remove(i) print(''.join(usercontent) + '\n')
複製 output 內容
最簡單的複製粘貼
select all: select the few words at the start, then CtrlShiftEnd