Issue
from bs4 import BeautifulSoup
raw_text = """<div style="margin: 0 0 15px 0;">
<b>Location:</b><br>
K23<br>
4225 Oaknoll Circle<br>Duluth, GA 30096
</div>"""
soup = BeautifulSoup(raw_text, "html.parser")
location_text = soup.find("b", text="Location:")
parent_location = location_text.parent
location_text_b_text = parent_location.find("b").extract()
location = parent_location.text.strip()
print("location: " + str(location))
Is there any solution to get the exact solution by removing spaces?
Required solution: K23 4225 Oaknoll Circle Duluth, GA 30096
Solution
After computing parent_location
, try removing all leading and trailing spaces from each line, and filters out empty lines, and then concatenate the refined lines into a single string.
location_lines = [line.strip() for line in parent_location.text.splitlines() if line.strip()]
location = ' '.join(location_lines)
Answered By - Beulah Evanjalin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.