Python3 - BeautifulSoup - Get value between two tags, where values between

Viewed 148

I have below HTML blocks, that are generated by pdftotext using the -bbox-layout option:

<flow>
<block xMin="21.600000" yMin="86.356000" xMax="178.647000" yMax="116.233001">
    <line xMin="21.600000" yMin="86.356000" xMax="178.647000" yMax="101.833000">
        <word xMin="21.600000" yMin="86.356000" xMax="178.647000" yMax="101.833000">
            My text string located here!</word>
    </line>

</block>
</flow>

[...]
<flow>
<block xMin="223.560000" yMin="323.675000" xMax="345.563500" yMax="339.855500">
    <line xMin="223.560000" yMin="323.675000" xMax="345.563500" yMax="339.855500">
        <word xMin="223.560000" yMin="323.675000" xMax="316.836500" yMax="339.855500">Another string
        </word>
        <word xMin="320.022000" yMin="323.675000" xMax="345.563500" yMax="339.855500">And another!</word>
    </line>
</block>
</flow>

Now, I am trying to dynamically parse the above structure, and get each <block>[...]</block> content, where the values xMin and xMax is between two numbers.

Imagine I have below numbers:

areas[0] = (100, 0, 200, 792)
areas[1] = (200, 0, 612, 792)

with open(path_to_html_document) as html_file:
    parsed_html = BeautifulSoup(html_file)
    for (i, area) in enumerate(areas):

        xMinValue, xMaxValue = areas[i][0], areas[i][2]

        block_tags = parsed_html.find_all(
            "block", attrs={"xMin": xMinValue, "xMax": xMaxValue})

        print(block_tags)

Above code doesn't return anything, because there are no matching tags. The find_all() search for exact matches for block tags with the specific numbers - but I am trying to search for block tags, where xMin and xMax is:

areas[0] is between 100 and 200

areas[1] is between 200 and 612

is this possible with BeautifulSoup?

3 Answers

Replace your code :

block_tags = parsed_html.find_all(
            "block", attrs={"xMin": xMinValue, "xMax": xMaxValue})
print(block_tags)

TO:

block_tags = parsed_html.find_all("block")

for block in block_tags:
    if float(block['xmin']) >= xMinValue and  float(block['xmax']) <= xMinValue:
        print(block)

If debug html code print(parsed_html), you will see html block all attribute in small letter.

try

parsed_html.select("block")

and filt the result with key "xMin" and "xMax".

For example, if you want to get <block xMin="1" xMax="2"></block>, you can first get all block tags by

all_blocks = parsed_html.select("block")

And you want to get one of the block whose xMin is 1 and xMax is 2, you can make it like:

target = filter(lambda x: x["xMin"] == "1" and x["xMax"] == 2, all_blocks)

You can select <block> with attributes xMin and xMax with CSS selector block[xMin][xMax]. Then you do filtering through list comprehension:

data = '''<flow>
<block xMin="21.600000" yMin="86.356000" xMax="178.647000" yMax="116.233001">
    <line xMin="21.600000" yMin="86.356000" xMax="178.647000" yMax="101.833000">
        <word xMin="21.600000" yMin="86.356000" xMax="178.647000" yMax="101.833000">
            My text string located here!</word>
    </line>

</block>
</flow>

<flow>
<block xMin="223.560000" yMin="323.675000" xMax="345.563500" yMax="339.855500">
    <line xMin="223.560000" yMin="323.675000" xMax="345.563500" yMax="339.855500">
        <word xMin="223.560000" yMin="323.675000" xMax="316.836500" yMax="339.855500">Another string
        </word>
        <word xMin="320.022000" yMin="323.675000" xMax="345.563500" yMax="339.855500">And another!</word>
    </line>
</block>
</flow>'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'lxml')

def blocks_min_max(soup, x_min, x_max):
    return [b for b in soup.select('block[xMin][xMax]') if float(b['xmin']) >= x_min and float(b['xmax']) <= x_max]

for b in blocks_min_max(soup, 10, 200):
    print(b.prettify())

Prints:

<block xmax="178.647000" xmin="21.600000" ymax="116.233001" ymin="86.356000">
 <line xmax="178.647000" xmin="21.600000" ymax="101.833000" ymin="86.356000">
  <word xmax="178.647000" xmin="21.600000" ymax="101.833000" ymin="86.356000">
   My text string located here!
  </word>
 </line>
</block>
Related