计算 div 标签的平均高度和平均宽度

compute the average height and the average width of div tag

我需要获取 html 文档的平均 div 高度和宽度。

我尝试过这个解决方案,但它不起作用:

import numpy as np
average_width = np.mean([div.attrs['width'] for div in my_doc.get_div() if 'width' in div.attrs])
average_height = np.mean([div.attrs['height'] for div in my_doc.get_div() if 'height' in div.attrs])
print average_height,average_width

get_div方法returnbeautifulSoup

find_all方法检索到的所有div的列表

这里有一个例子:

print my_doc.get_div()[1]

<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:45px; top:81px; width:127px; height:9px;">
    <span style="font-family: EICMDA+AdvTrebu-R; font-size:8px">Journal of     Infection (2015) 
    </span>
    <span style="font-family: EICMDB+AdvTrebu-B; font-size:8px">xx</span>
    <span style="font-family: EICMDA+AdvTrebu-R; font-size:8px">, 1</span>
    <span style="font-family: EICMDD+AdvPS44A44B; font-size:7px">e</span>
    <span style="font-family: EICMDA+AdvTrebu-R; font-size:8px">4
    <br/>
    </span>
</div>

当我得到属性时,它完美地工作了

print my_doc.get_div()[1].attrs

{u'style': u'position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:45px; top:81px; width:127px; height:9px;'}

但是当我尝试获取值时

print my_doc.get_div()[1].attrs['width']

我收到一个错误:

KeyError: 'width'

但我不明白,因为当我检查类型时:

print type(my_doc.get_div()[1].attrs)

这是一本字典,<type 'dict'>

可能有更好的方法-

方式-1

下面是我测试过的提取 widthheight 的代码。

from bs4 import BeautifulSoup

html_doc = '''<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:45px; top:81px; width:127px; height:9px;">
    <span style="font-family: EICMDA+AdvTrebu-R; font-size:8px">Journal of     Infection (2015) 
    </span>
    <span style="font-family: EICMDB+AdvTrebu-B; font-size:8px">xx</span>
    <span style="font-family: EICMDA+AdvTrebu-R; font-size:8px">, 1</span>
    <span style="font-family: EICMDD+AdvPS44A44B; font-size:7px">e</span>
    <span style="font-family: EICMDA+AdvTrebu-R; font-size:8px">4
    <br/>
    </span>
</div>'''

soup = BeautifulSoup(html_doc,'html.parser')    
my_att = [i.attrs['style'] for  i in soup.find_all("div")]
dd = ''.join(my_att).split(";")
dd_cln= filter(None, dd)
dd_cln= [i.strip() for i in dd_cln ]
my_dict = dict(i.split(':') for i  in dd_cln)
print my_dict['width']

方式二 按照 here.

所述使用正则表达式

工作代码-

import numpy as np
import re
from bs4 import BeautifulSoup

html_doc = '''<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:45px; top:81px; width:127px; height:9px;">
    <span style="font-family: EICMDA+AdvTrebu-R; font-size:8px">Journal of     Infection (2015) 
    </span>
    <span style="font-family: EICMDB+AdvTrebu-B; font-size:8px">xx</span>
    <span style="font-family: EICMDA+AdvTrebu-R; font-size:8px">, 1</span>
    <span style="font-family: EICMDD+AdvPS44A44B; font-size:7px">e</span>
    <span style="font-family: EICMDA+AdvTrebu-R; font-size:8px">4
    <br/>
    </span>
</div>'''

soup = BeautifulSoup(html_doc,'html.parser')    
my_att = [i.attrs['style'] for  i in soup.find_all("div")]
css = ''.join(my_att)
print css
width_list = map(float,re.findall(r'(?<=width:)(\d+)(?=px;)', css))
height_list = map(float,re.findall(r'(?<=height:)(\d+)(?=px;)', css))
print np.mean(height_list)
print np.mean(width_list)