如何使用beautifulsoup快速操作div内的跨度

时间:2018-09-04 06:04:17

标签: python beautifulsoup

我从 HOCR 工具获得了 HTML 输出,我想在divocr_carea上应用以下操作。

编辑的输入HTML

div文件中的

HTML标签看起来像这样。

<div class='ocr_carea' id='block_1_8' title="bbox 95 779 341 884">
    <p class='ocr_par' id='par_1_16' lang='Latin' title="bbox 95 779 341 884">
     <span class='ocr_line' id='line_1_29' title="bbox 96 779 338 800; baseline 0 -1; x_size 25.636646; x_descenders 5.6366458; x_ascenders 5">
      <span class='ocrx_word' id='word_1_62' title='bbox 96 779 186 800; x_wconf 96'>Header</span>
      <span class='ocrx_word' id='word_1_63' title='bbox 195 779 338 799; x_wconf 96'>Information</span>
     </span>
     <span class='ocr_line' id='line_1_30' title="bbox 96 819 341 839; baseline 0 0; x_size 25.26087; x_descenders 5.2608695; x_ascenders 6">
      <span class='ocrx_word' id='word_1_64' title='bbox 96 819 212 839; x_wconf 96'>Purchase</span>
      <span class='ocrx_word' id='word_1_65' title='bbox 221 819 290 839; x_wconf 96'>Order</span>
      <span class='ocrx_word' id='word_1_66' title='bbox 300 819 341 839; x_wconf 96'>No:</span>
     </span>
     <span class='ocr_line' id='line_1_31' title="bbox 95 859 334 884; baseline -0.004 -4; x_size 26; x_descenders 5; x_ascenders 7">
      <span class='ocrx_word' id='word_1_67' title='bbox 95 859 175 880; x_wconf 96'>Terms</span>
      <span class='ocrx_word' id='word_1_68' title='bbox 185 859 210 880; x_wconf 96'>of</span>
      <span class='ocrx_word' id='word_1_69' title='bbox 218 859 334 884; x_wconf 96'>Payment:</span>
     </span>
    </p>
   </div>
   <div class='ocr_carea' id='block_1_9' title="bbox 371 819 542 840">
    <p class='ocr_par' id='par_1_17' lang='Latin' title="bbox 371 819 542 840">
     <span class='ocr_line' id='line_1_32' title="bbox 371 819 542 840; baseline 0.006 -1; x_size 27.5; x_descenders 6.875; x_ascenders 6.875">
      <span class='ocrx_word' id='word_1_70' title='bbox 371 819 542 840; x_wconf 96'>4056111455</span>
     </span>

我想将它们连接起来并像这样正确排序

 <div class='ocr_carea' id='block_1_8' title="bbox 95 779 341 884">
    <p class='ocr_par' id='par_1_16' lang='Latin' title="bbox 95 779 341 884">
     <span class='ocr_line' id='line_1_29' title="bbox 96 779 338 800; baseline 0 -1; x_size 25.636646; x_descenders 5.6366458; x_ascenders 5">
      <span class='ocrx_word' id='word_1_62' title='bbox 96 779 186 800; x_wconf 96'>Header</span>
      <span class='ocrx_word' id='word_1_63' title='bbox 195 779 338 799; x_wconf 96'>Information</span>
     </span>
     <span class='ocr_line' id='line_1_30' title="bbox 96 819 341 839; baseline 0 0; x_size 25.26087; x_descenders 5.2608695; x_ascenders 6">
      <span class='ocrx_word' id='word_1_64' title='bbox 96 819 212 839; x_wconf 96'>Purchase</span>
      <span class='ocrx_word' id='word_1_65' title='bbox 221 819 290 839; x_wconf 96'>Order</span>
      <span class='ocrx_word' id='word_1_66' title='bbox 300 819 341 839; x_wconf 96'>No:</span>
      <span class='ocrx_word' id='word_1_70' title='bbox 371 819 542 840; x_wconf 96'>4056111455</span>
     </span>
     </span>
     <span class='ocr_line' id='line_1_31' title="bbox 95 859 334 884; baseline -0.004 -4; x_size 26; x_descenders 5; x_ascenders 7">
      <span class='ocrx_word' id='word_1_67' title='bbox 95 859 175 880; x_wconf 96'>Terms</span>
      <span class='ocrx_word' id='word_1_68' title='bbox 185 859 210 880; x_wconf 96'>of</span>
      <span class='ocrx_word' id='word_1_69' title='bbox 218 859 334 884; x_wconf 96'>Payment:</span>
     </span>
    </p>
   </div>

我认为这可以通过BautifulSoup来完成,到目前为止,我已经实现了在列表中添加跨度ocr_line,我想搜索跨度ocr_line并检查是否bbox彼此​​靠近,在x轴或y轴上向上或向下移动了一个点

from bs4 import BeautifulSoup
soup = BeautifulSoup(hocr_container,'html.parser')
lines = soup.find_all('span',class_='ocr_line')
for line in lines
# Check the bbox and concatenate span

1 个答案:

答案 0 :(得分:1)

这可能对您有帮助

from bs4 import BeautifulSoup
html = """
      <div class='ocr_carea' id='block_1_8' title="bbox 95 779 341 884">
<p class='ocr_par' id='par_1_16' lang='Latin' title="bbox 95 779 341 884">
 <span class='ocr_line' id='line_1_29' title="bbox 96 779 338 800; baseline 0 -1; x_size 25.636646; x_descenders 5.6366458; x_ascenders 5">
  <span class='ocrx_word' id='word_1_62' title='bbox 96 779 186 800; x_wconf 96'>Header</span>
  <span class='ocrx_word' id='word_1_63' title='bbox 195 779 338 799; x_wconf 96'>Information</span>
 </span>
 <span class='ocr_line' id='line_1_30' title="bbox 96 819 341 839; baseline 0 0; x_size 25.26087; x_descenders 5.2608695; x_ascenders 6">
  <span class='ocrx_word' id='word_1_64' title='bbox 96 819 212 839; x_wconf 96'>Purchase</span>
  <span class='ocrx_word' id='word_1_65' title='bbox 221 819 290 839; x_wconf 96'>Order</span>
  <span class='ocrx_word' id='word_1_66' title='bbox 300 819 341 839; x_wconf 96'>No:</span>
  <span class='ocrx_word' id='word_1_70' title='bbox 371 819 542 840; x_wconf 96'>4056111455</span>
 </span>
 </span>
 <span class='ocr_line' id='line_1_31' title="bbox 95 859 334 884; baseline -0.004 -4; x_size 26; x_descenders 5; x_ascenders 7">
  <span class='ocrx_word' id='word_1_67' title='bbox 95 859 175 880; x_wconf 96'>Terms</span>
  <span class='ocrx_word' id='word_1_68' title='bbox 185 859 210 880; x_wconf 96'>of</span>
  <span class='ocrx_word' id='word_1_69' title='bbox 218 859 334 884; x_wconf 96'>Payment:</span>
 </span>
</p>
</div>"""

 soup = BeautifulSoup(html, 'html.parser')
 tag = soup.find_all('span', attrs={'class':'ocr_line'})
 for i in tag:
     x = (' '.join(i.stripped_strings))
     print x