0

I am trying to retrieve the 'Sold for' value (i.e. $1,040,000) and 'Rating value' (i.e. $640,000) from the below piece of html code.

Here is the code I'm using:

html = '''
              <div class="padb-listing-id">
               <span>
                Listing ID:
               </span>
               804244
              </div>
              <div class="padb-property-value">
               <span>
                Sold for:
               </span>
               $1,040,000
              </div>
              <div class="padb-property-rating-value">
               <span>
                Rating Value:
               </span>
               $640,000
               <span class="padb-rating-date">
                (July '17)
               </span>
              </div>
             '''

from bs4 import BeautifulSoup
import requests

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

listing_id = soup.find('div', class_='padb-listing-id').text
value = soup.find('div', class_='padb-property_value')
rating_value = soup.find('div', class_='padb-property-rating-value').text
print(f'{listing_id}Price: {value}{rating_value}')

which returns this output:

        Listing ID:
       
       804244
      Price: None

        Rating Value:
       
       $640,000
       
        (July '17)
           

It brings back 'None' for the 'Sold for' value and I don't know how to fix it. I've looked up a few posts link1, link2, link3 but none of these have worked for me.

I'm new to BeautifulSoup and html so would really appreciate some help.

Thanks!!

P.s. When I add '.text' to the 'value' code I get an AttributeError: 'NoneType' object has no attribute 'text'. Thought I add this here in case anyone wonders why the 'value' code is different from the rest.

JanineKNZ
  • 35
  • 4

1 Answers1

0

It is just a typo in value = soup.find('div', class_='padb-property-value')

Set class name to:

padb-property-value

And add .text:

value = soup.find('div', class_='padb-property-value').text

Just in case

If you only want the value of the price you can do the following things.

  1. Split your actually value:

    soup.find('div', class_='padb-property-value').text.split()[-1]        
    
  2. Specify your selection:

    soup.find('div', class_='padb-property-value').contents[2].strip()
    
HedgeHog
  • 22,146
  • 4
  • 14
  • 36
  • Thanks!! How silly of me, sorry for not spotting that myself. Thanks for the extra tips as well! The output looks much nicer now!! – JanineKNZ Jan 18 '21 at 07:53
  • Happy to help - Sometimes I also do not see the wood for the trees ... :) – HedgeHog Jan 18 '21 at 08:15