Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract information with the same tag

For the Zillow data below, number of beds (bds), number of bath (ba) and square foot (sqfr) have the same tag <li class="">.
How can I get information for these 3 elements. My code below is clearly not working.
The result should be: 3 , 2, 1813

Can you please help?
Thanks
Hong

<div class="list-card-info"><a class="list-card-link list-card-link-top-margin" href="https://www.zillow.com/homedetails/12021-Tralee-Rd-UNIT-102-Lutherville-MD-21093/60873148_zpid/" tabindex="0">
# <address class="list-card-addr">12021 Tralee Rd UNIT 102, Lutherville, MD 21093</address></a>
# <div class="list-card-footer"><p class="list-card-extra-info">LONG &amp; FOSTER REAL ESTATE, INC.</p></div><div class="list-card-heading">
# <div class="list-card-price">$411,000</div><ul class="list-card-details">
# <li class="">3<abbr class="list-card-label"> <!-- -->bds</abbr></li>
# <li class="">2<abbr class="list-card-label"> <!-- -->ba</abbr></li>
# <li class="">1,813<abbr class="list-card-label"> <!-- -->sqft</abbr>
# </li><li class="list-card-statusText">- Apartment for sale</li></ul></div></div>

tag='<div class="list-card-info"><a class="list-card-link list-card-link-top-margin" href="https://www.zillow.com/homedetails/12021-Tralee-Rd-UNIT-102-Lutherville-MD-21093/60873148_zpid/" tabindex="0"><address class="list-card-addr">12021 Tralee Rd UNIT 102, Lutherville, MD 21093</address></a><div class="list-card-footer"><p class="list-card-extra-info">LONG &amp; FOSTER REAL ESTATE, INC.</p></div><div class="list-card-heading"><div class="list-card-price">$411,000</div><ul class="list-card-details"><li class="">3<abbr class="list-card-label"> <!-- -->bds</abbr></li><li class="">2<abbr class="list-card-label"> <!-- -->ba</abbr></li><li class="">1,813<abbr class="list-card-label"> <!-- -->sqft</abbr></li><li class="list-card-statusText">- Apartment for sale</li></ul></div></div>'
tag = BeautifulSoup(tag, 'html.parser')

address = tag.findAll('address', {'class': 'list-card-addr'})
price   = tag.findAll('div', {'class': 'list-card-price'})
beds    = tag.findAll('li', {'class': ""}) 

# keep text only, remove tag
address=address[0].text; 
price=price[0].text ;
beds=beds[0].text; print(beds)
print(address, '---',price, '---',beds)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When you call tag.findAll it creates a ResultSet with all three values saved. You can then access each one using the index number, as shown below.

from bs4 import BeautifulSoup

tag= '<div class="list-card-info"><a class="list-card-link list-card-link-top-margin" href="https://www.zillow.com/homedetails/12021-Tralee-Rd-UNIT-102-Lutherville-MD-21093/60873148_zpid/" tabindex="0"><address class="list-card-addr">12021 Tralee Rd UNIT 102, Lutherville, MD 21093</address></a><div class="list-card-footer"><p class="list-card-extra-info">LONG &amp; FOSTER REAL ESTATE, INC.</p></div><div class="list-card-heading"><div class="list-card-price">$411,000</div><ul class="list-card-details"><li class="">3<abbr class="list-card-label"> <!-- -->bds</abbr></li><li class="">2<abbr class="list-card-label"> <!-- -->ba</abbr></li><li class="">1,813<abbr class="list-card-label"> <!-- -->sqft</abbr></li><li class="list-card-statusText">- Apartment for sale</li></ul></div></div>'

tag = BeautifulSoup(tag, 'html.parser')

tags = tag.findAll('li', {'class': ""})

# keep text only, remove tag
address=tags[0].text;
price=tags[1].text ;
beds=tags[2].text;
print(address, '---',price, '---',beds)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading