The title is a mouthful but describes what I want. I am parsing through an XML with BeautifulSoup. The format of my XML is as follows:
<properties>
<place>
<house_id>12345</house_id>
<appliances>Fridge, Oven</appliances>
<price>350000</price>
</place>
<place>
<house_id>6789</house_id>
<appliances>Heater, Microwave, Fridge</appliances>
<price>870000</price>
</place>
</properties>
Given a specific value for the house_id
tag, I want the text INSIDE of the appliances
that correspond to that place. For instance, given 12345
, I want to return Fridge, Oven
. I have not found an easy way to do this yet with BeautifulSoup.
CodePudding user response:
Based on your input XML, the following XPath expression will produce what you need.
can we use XPath with BeautifulSoup?
XPath
/properties/place[house_id="12345"]/appliances
CodePudding user response:
You can use the General sibling (~
) CSS selector:
soup.select_one("house_id:-soup-contains('12345') ~ appliances").text
Or you can find the <house_id>
tag containing specific text, and then call find_next()
to locate the <appliances>
tag:
print(soup.find("house_id", text="12345").find_next("appliances").text)