Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用beautifulsoup快速操纵div内的span_Python_Beautifulsoup - Fatal编程技术网

Python 如何使用beautifulsoup快速操纵div内的span

Python 如何使用beautifulsoup快速操纵div内的span,python,beautifulsoup,Python,Beautifulsoup,我有HOCR工具的HTML输出,我想在div类ocr_carea上应用以下操作 编辑输入HTML HTML文件中的div标记,如下所示 <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">

我有HOCR工具的HTML输出,我想在div类ocr_carea上应用以下操作

编辑输入HTML HTML文件中的div标记,如下所示

<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>
    
这可能对你有帮助

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

很抱歉,输入的HTML添加错误。我现在改正了。我的想法是,我有两个跨度,我想根据conditionAFAIK合并它们。获取元素的最快方法是通过它的ID选择器。您可以为所需ID编写一个正则表达式,并通过该正则表达式获取所有ocrx_单词和ocrx_行元素,然后解析它们。这里可以找到一个解决方案:我已经检查了这个问题,但我的问题不是解析它们,而是合并跨距。你是说用找到并处理的BS元素重新构建页面吗?如果是这样,您可以使用BS的元素字符串表示形式。只需调用找到的strwhatever\u元素,并在解析和排序时将其结果附加到生成的文件中。这里有示例和文档,所以我们没有查找和替换方法或移位技术
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
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