BeautifulSoup: Print div's based on content of preceding tag

我的梦境 提交于 2020-01-13 19:22:46

问题


I would like to select the contents of elements based on the preceding tag:

<h4>Models &amp; Products</h4>
    <div class="profile-area">...</div>

<h4>Production Capacity (year)</h4>
    <div class="profile-area">...</div>

How can I get the "profile-area" values based on the content of the preceding tag?

Here is my code:

import requests
from bs4 import BeautifulSoup
import csv
import re

html_doc = """
<html>
<body>
  <div class="col-md-6">
    <iframe class="factory_detail_google_map" frameborder="0" src=
    "https://www.google.com/maps/embed/v1/search?q=3.037787%2C101.38189&amp;key=AIzaSyCMDADp9QHYbQ8OBGl8puAOv-16W8ziz7Y"
    allowfullscreen=""></iframe>
  </div>

  <div class="col-md-12">
    <h4>Models &amp; Products</h4>

    <div class="profile-area">
      Large Buses, Trucks, Trailer-heads
    </div>

    <h4>Production Capacity (year)</h4>

    <div class="profile-area">
      Vehicle 700 units /year
    </div>

    <h4>Output</h4>

    <div class="profile-area">
      Vehicle 356 units ( 2016 )
    </div>

    <div class="profile-area">
      Vehicle 477 units ( 2015 )
    </div>

    <div class="profile-area">
      Vehicle 760 units ( 2014 )
    </div>

    <div class="profile-area">
      Vehicle 647 units ( 2013 )
    </div>
  </div>
</body>
</html>
"""
soup = BeautifulSoup(html, 'lxml')

#link=soup.iframe.get('src')
#print(link.split("%2C"))

for item in soup.select("div.profile-area"):
    print(item.text)

As you can see I'm also trying to split the Google Maps link into coordinates, but this I will figure out probably on my own.

Thanks for your help!


回答1:


Use .find_previous_sibling() to explicitly find the first preceding h4 tag:

for item in soup.select("div.profile-area"):
    prev_h4 = item.find_previous_sibling('h4').text
    if 'Capacity' in prev_h4:
        print(item.text)

Output

Vehicle 700 units /year


来源:https://stackoverflow.com/questions/50589151/beautifulsoup-print-divs-based-on-content-of-preceding-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!