Issue
I'm scraping the following HTML:
<h2>Address</h2>
<p class="pp-property-price">£$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 class="pp-property-box">
<h2>Address</h2>
<p class="pp-property-price">£$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
Solution
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'
Answered By - Greg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.