I am trying to scrape https://www.trulia.com/sold/32303_zip/6_srl/, I simply need the number of homes sold which is stated in the ...."sold homes on Trulia" sentence at the top right. The code below sometimes gets the number, other times it gets "Nearby" from the "Nearby Real Estate" from the bottom, which is another h2 element. What is wrong or missing from my code?
url = "https://www.trulia.com/sold/32303_zip/6_srl/"
html = requests.get(url)
html = html.content
soup = BeautifulSoup(html, 'html.parser')
soup.get_text()
s = soup.div.h2.get_text()
s = s.split()
s = s[0]
s = s.replace(',', '')
