Как удалить или выровнять пробелы по левому краю из данных, очищенных с помощью BeautifulSoup? Данные конвертируются в str из юникода.
попробовал str.strip() и str.ljust(), str.replace("\n",""). Проверка каждого символа на None и отображение, если нет None, также не работает
from bs4 import BeautifulSoup
import requests
with open('f.html') as f:
soup=BeautifulSoup(f,'lxml')
article = soup.findAll('div',class_='modal-content')
for i in article:
print (str(i.text).strip())
Вывод получил:
reset to default listTour
Start here for a quick overview of the site
Help Center
Detailed answers to any questions you might have
ожидаемый результат:
reset to default listTour
Start here for a quick overview of the site
Help Center
Detailed answers to any questions you might have
Это некрасиво, но я думаю, что это сработает.
from bs4 import BeautifulSoup
import requests
with open('f.html') as f:
soup=BeautifulSoup(f,'lxml')
article = soup.findAll('div',class_='modal-content')
for i in article:
text=str(i.text)
text=text.split('\n')
text=[x.strip() for x in text if x.strip()!='']
output=''
for t in text:
output+=t+'\n'
print(output)