0

I am scraping a site with python3 using beautiful Soup. I store my data into a list.

I manage to extract the info that i want

import requests
from bs4 import BeautifulSoup

source = requests.get('my site').text
soup = BeautifulSoup(source, 'lxml')

lista = []

rows = soup.find('table', class_='exchange-rates-table not- 
responsive').find_all('tr')

for row in rows:          # Print all occurrences
    l.ista.append(row.contents[3].get_text())
print(lista)



This is the output:

['Cod', '\n\n                        EUR\n\n                    ', 
'\n\n                        USD\n\n                    ', '\n\n                        
GBP\n\n                    ', '\n\n                        CHF\n\n                    
', '\n\n                        AUD\n\n                    ', '\n\n                        
DKK\n\n                    ', '\n\n                        HUF\n\n                    
', '\n\n                        JPY\n\n                    ', '\n\n                        
NOK\n\n                    ', '\n\n                        SEK\n\n                    
']

When i run this code i am receiving the info that i want, but with a lot of empty spaces with comma between them and the sign of new line. So how can i remove them to get only what i want.

pppery
  • 3,731
  • 22
  • 33
  • 46
mihai
  • 13
  • 2

1 Answers1

0

Since your data is already in a list, you can do with strip in a list comprehension:

[x.strip() for x in ['Cod', '\n\n EUR\n\n ', '\n\n USD\n\n ', '\n\n GBP\n\n ', '\n\n CHF\n\n ', '\n\n AUD\n\n ', '\n\n DKK\n\n ', '\n\n HUF\n\n ', '\n\n JPY\n\n ', '\n\n NOK\n\n ', '\n\n SEK\n\n ']]
  • strip is the keyword, but look at the answer that i need: 'lista = [] for row in rows: lista.append(row.contents[i].text.strip())' – mihai Aug 20 '19 at 20:43