计算div标签

时间:2015-10-15 14:27:34

标签: python html beautifulsoup

我需要获得html doc的平均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方法返回由beautifulSoup的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 个答案:

答案 0 :(得分:1)

可能有更好的方法 -

方式-1

以下是我测试的代码,用于提取宽度高度

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']

<强>路-2 使用正如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)