How to remove multiple empty rows when crawling with Beautifulsoup… here is a solution to the problem.
How to remove multiple empty rows when crawling with Beautifulsoup
My code outputs multiple empty line breaks.
How do I remove all white space?
from bs4 import BeautifulSoup
import urllib.request
import re
url = input('enter url moish')
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page,'lxml')
all = soup.find_all('a', {'class' : re.compile('itemIncludes')})
for i in all:
print(i.text)
Code output:
Canon EOS 77D DSLR Camera (Body Only)
LP-E17 Lithium-Ion Battery Pack
LC-E17 Charger for LP-E17 Battery Pack
Expected output:
Canon EOS 77D DSLR Camera (Body Only)
LP-E17 Lithium-Ion Battery Pack
LC-E17 Charger for LP-E17 Battery Pack
Thanks!
Solution
You can remove empty lines before printing:
items = [item.text for item in all if item.text.strip() != '']