使用<p>美丽汤定位具有自动类名的类名进行网络抓取



我正在抓取以下HTML:

<h2>Address</h2>
<p class="pp-property-price">£$150,000></p>
<p>5 bedroom mansion</p>

到目前为止,我的Python对于h2和第一个<p>的外观和工作方式与下面类似。我如何针对后面的<p>,因为它没有我们可以针对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

如果要查找类property-price之后的下一个标记,请尝试find_next

li.find('p', class_="pp-property-price").find_next('p').text

输出:"五居室豪宅">

最新更新