首页 > 解决方案 > 如何获取html文档中字符的坐标?

问题描述

<span class = 'ocrx_word' id = 'word_1_45' title = 'bbox 369 429 301 123;x_wconf 96'>refrence</span>

如何使用 python 从上述代码中仅提取 369 429 301 123 值?

标签: pythonpython-3.xweb-scrapingbeautifulsouppython-tesseract

解决方案


解决这个问题的最简单方法是最有可能用分号分割文本以获取之前的所有内容。然后您可以再次拆分并仅保留数字部分。

from bs4 import BeautifulSoup

tag = "<span class = 'ocrx_word' id = 'word_1_45' title = 'bbox 369 429 301 123;x_wconf 96'>refrence</span>"
soup = BeautifulSoup(tag, 'html.parser')
s = soup.findAll('span')

for span in s:
    print([x  for x in span.attrs['title'].split(';')[0].split() if x.isdigit()])

推荐阅读