简体   繁体   中英

Parse and sort html tags with beautiful soup

I have below HTML file, which contains bbox information from a PDF file:

<flow>
  <block xMin="53.879997" yMin="369.965298" xMax="63.939976" yMax="380.991433">
    <line xMin="53.879997" yMin="369.965298" xMax="63.939976" yMax="380.991433">
      <word xMin="53.879997" yMin="369.965298" xMax="63.939976" yMax="380.991433">10</word>
    </line>
  </block>
</flow>
<flow>
  <block xMin="53.879997" yMin="417.965298" xMax="63.939976" yMax="428.991433">
    <line xMin="53.879997" yMin="417.965298" xMax="63.939976" yMax="428.991433">
      <word xMin="53.879997" yMin="417.965298" xMax="63.939976" yMax="428.991433">20</word>
    </line>
  </block>
</flow>
<flow>
  <block xMin="111.351361" yMin="369.965298" xMax="134.220382" yMax="380.991433">
    <line xMin="111.351361" yMin="369.965298" xMax="134.220382" yMax="380.991433">
      <word xMin="111.351361" yMin="369.965298" xMax="116.331548" yMax="380.991433">1</word>
      <word xMin="121.909358" yMin="369.965298" xMax="134.220382" yMax="380.991433">PC</word>
    </line>
  </block>
</flow>

Above is the bounding box areas for the words: 10 20 1 PC

In the original document, it is written like this:

10 1 PC
20

Hence, I would like to parse above HTML file and extract all <line> tags, and then sort them all by the yMin value. The end output of above would then be: 10 1 PC 20 instead.

What I've tried so far

I am not very far, as I am still learning Python. I am using BeautifulSoup4:

with open("test.html", "r") as f:
    contents = f.read()
    soup = BeautifulSoup(contents, 'lxml')

    for line in soup.find_all("line", attrs={"ymin":True}):
        print(line.get('ymin'))

Above simply prints out each tag and it's content.

I am unsure how I can sort the line tags though.

Any help would be highly appreciated.

You can use BeautifulSoup with soup.find_all :

from bs4 import BeautifulSoup as soup
r = [i.find_all('word') for i in sorted(soup(html, 'html.parser').find_all('line'), key=lambda x:float(x['ymin']))]
result = [i.text for b in r for i in b]

Output:

['10', '1', 'PC', '20']

Try the below code.Can can define the mean value and then check with mean value.

from bs4 import BeautifulSoup
html='''<flow>
  <block xMin="53.879997" yMin="369.965298" xMax="63.939976" yMax="380.991433">
    <line xMin="53.879997" yMin="369.965298" xMax="63.939976" yMax="380.991433">
      <word xMin="53.879997" yMin="369.965298" xMax="63.939976" yMax="380.991433">10</word>
    </line>
  </block>
</flow>
<flow>
  <block xMin="53.879997" yMin="417.965298" xMax="63.939976" yMax="428.991433">
    <line xMin="53.879997" yMin="417.965298" xMax="63.939976" yMax="428.991433">
      <word xMin="53.879997" yMin="417.965298" xMax="63.939976" yMax="428.991433">20</word>
    </line>
  </block>
</flow>
<flow>
  <block xMin="111.351361" yMin="369.965298" xMax="134.220382" yMax="380.991433">
    <line xMin="111.351361" yMin="369.965298" xMax="134.220382" yMax="380.991433">
      <word xMin="111.351361" yMin="369.965298" xMax="116.331548" yMax="380.991433">1</word>
      <word xMin="121.909358" yMin="369.965298" xMax="134.220382" yMax="380.991433">PC</word>
    </line>
  </block>
</flow>'''

soup=BeautifulSoup(html,'lxml')
pricemin=soup.select_one('line[yMin]')['ymin']
list1=[]
list_last=[]
for item in soup.select('line[yMin]'):
    if float(pricemin) < float(item['ymin']):

         for w in item.select('word'):
             list_last.append(w.text)
    else:
        for w in item.select('word'):
            list1.append(w.text)

print(list1+list_last)

Output :

['10', '1', 'PC', '20']

To print this

print(' '.join(list1+list_last))

Output :

10 1 PC 20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM