beautifulsoup get text by spaces between span tag
I have this code:
rr = []
bs_add = result.find('span', {'class': 'address'})#.text#.replace('\n', ' ')
print(bs_add)
print(bs_add.attrs)
print(bs_add.get_text())
and this code gives me result like that:
<span class="address"><span>595 Buckingham Way Ste 331</span>San Francisco, CA 94132</span>
{'class': ['address']}
595 Buckingham Way Ste 331San Francisco, CA 94132
the problem in this section: gives me the result: 595 Buckingham Way Ste 331San Francisco, CA 94132
but I need a result like that: 595 Buckingham Way Ste 331 San Francisco, CA 94132
.
Solution 1:
How to achieve?
You are close to your goal, just specify a character or string for your .get_text()
method to join the bits of extracted text together - In this case a whitespace:
bs_add.get_text(' ', strip=True)
or use stripped_strings
for same result:
' '.join(bs_add.stripped_strings)
Output
595 Buckingham Way Ste 331 San Francisco, CA 94132