I'm scraping the following HTML:
<h2>Address</h2>
<p >£$150,000></p>
<p>5 bedroom mansion</p>
My Python so far looks and works like the below for the h2
and the first <p>
. How can I target the <p>
that comes after as it doesn't have a classname we can target on Beautiful Soup?
from bs4 import BeautifulSoup
html = '''<li >
<h2>Address</h2>
<p >£$150,000></p>
<p>5 bedroom mansion</p></li>'''
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all("li")
for li in soup.find_all('li', class_="pp-property-box"):
title = li.find('h2').text
price = li.find('p', class_="pp-property-price").text
CodePudding user response:
If you want to find the next tag after the class property-price
, then try find_next
li.find('p', class_="pp-property-price").find_next('p').text
Output: '5 bedroom mansion'