我需要获取html文档的平均div高度和宽度。计算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
方法返回所有的列表DIV通过beautifulSoup
这里的find_all
方法检索是一个例子:
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'>
?你可以给网页或更多的HTML页面的源? – SIslam
@SIslam,我编辑了我的帖子 –
你如何计算'div'的宽度?例如:我有一个'div'设置为100%宽度。如果我的窗口是全屏的话,大概是〜1900px。如果我的窗口更小,'div'更小。那么它的宽度是多少? '平均'这个概念是怎么来的? –