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

2023-11-30

我需要获取 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

the get_div方法返回由 检索到的所有 div 的列表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'>


也许还有更好的办法——

Way -1

下面是我测试过的提取代码width and height.

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

Way-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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

计算div标签的平均高度和平均宽度 的相关文章

  • 使用时间序列数据和scaleBand指定D3条形图上的刻度

    我尝试为具有时间序列数据的 d3 v4 条形图指定多个刻度和多个刻度标签 如下图所示 基于本教程 https bl ocks org zigahertz 1ee4965ff76514517bb7ce6af21e5d44我有一个处理时间序列数
  • 如何使单词中的每个字母在悬停时发生变化

    假设我的网站上某个段落中有一个单词 IamGreat 我希望它在悬停时更改为 Good4you 但是 我不想更改整个单词 而是希望每个字母单独更改 因此 如果我将鼠标悬停在字母 I 上 它将变成字母 G 字母 r 将变成数字 4 等 这两个
  • 从提交的表单中转义字符串中的字符

    每次发布帖子时 我都会得到转义字符 gt gt gt 我有一个多步骤表单 它将数据从一种表单传输到另一种表单 我将这些值与准备好的语句一起保存在数据库中 数据库中的值当前看起来像Paul s House 用户应该可以在字符串中使用单引号和双
  • 小数缓存是Python规范中定义的还是一个实现细节?

    Python 似乎有一个所谓的 小数字缓存 用于存储 5 到 256 范围内的数字 我们可以使用以下程序来演示这一点 for i in range 7 258 if id i id i 0 print i is cached else pr
  • Django 未在 404 页面上应用应用程序中的 CSS 文件

    姜戈3 0 8 Python 3 7 x 我有一个包含一些应用程序的 Django 项目 我正在尝试为 400 403 404 500 错误制作一些 默认 错误页面 我已经这样做了 并显示了适当的模板 但没有任何样式或 JS 在 404 错
  • 等待动态加载脚本

    在我的页面正文中 我需要插入以下代码作为 AJAX 调用的结果 p Loading jQuery p p Using jQuery p 我不能使用 load 由于文档已经加载 因此该事件不会触发 这安全吗 如果没有 我如何确保在执行自定义生
  • 下拉菜单导致滚动条

    我用过这个W3C 的示例 http www w3schools com bootstrap bootstrap dropdowns asp div class dropdown div
  • 是否有更矢量化的方法来沿轴执行 numpy.outer ?

    gt gt gt x np array a0 a1 b0 b1 gt gt gt y np array x0 x1 y0 y1 gt gt gt iterable np outer x i y i for i in xrange x sha
  • 使用 python 只读取 Excel 中的可见行

    我想只读取 python 中 Excel 工作表中的可见行 输入 Excel表 所以当我过滤时 作为 python 中的输出 在本例中我将仅获得可见数据 1 行 这是我的代码 from openpyxl import load workbo
  • python:是否有用于对输入流进行分块的库函数?

    我想对输入流进行分块以进行批处理 给定一个输入列表或生成器 x in 1 2 3 4 5 6 我想要一个能够返回该输入块的函数 说 如果chunk size 4 then x chunked 1 2 3 4 5 6 这是我一遍又一遍地做的事
  • 将压缩的json数据存储在本地存储中

    我想将 JSON 数据存储在本地存储中 有时存储的数据可能超过 5MB 每个域的浏览器允许的最大阈值 无论如何 我可以压缩或压缩数据并将其存储在本地存储中吗 如果对大数据进行每个 JS 函数的压缩和解压 会增加多少延迟 我正在使用这个 js
  • Python,多线程,获取网页,下载网页

    我想在一个站点批量下载网页 我的 urls txt 文件中有 5000000 个 url 链接 大约有300M 如何让多线程链接这些网址并下载这些网页 或者如何批量下载这些网页 我的想法 with open urls txt r as f
  • 适用于 HTML5 混合应用程序的 CORS

    我读过很多关于 CORS 的文章 以及允许 Access Control Allow Origin 如何成为 Web 服务器的安全漏洞 但没有一篇文章解释了如何允许 HTML5 混合应用程序访问某些不允许使用通配符 的域上托管的 Web 服
  • Python:帮助(numpy)在退出时导致段错误

    我遇到了一个奇怪的现象 在 python 解释器中 我执行以下操作 gt gt gt import numpy gt gt gt help numpy 帮助显示正确 但一旦我按 q 返回解释器 Segmentation fault core
  • Scrapy的redirect_urls异常.KeyError

    我是 Scrapy 和 Python 的新手 最近推出了我的第一个蜘蛛 有一个功能似乎以前有效 但现在它只适用于我试图废弃的一些网站 代码行是 item url direct response request meta redirect u
  • 创建响应式眼睛焦点图标

    我一直在尝试制作响应式彩色眼睛焦点图标 但到目前为止我所尝试的一切均不成功 我试图在某种程度上复制真眼的颜色 我使用边框 框阴影来获取颜色 但该部分没有缩放 也尝试过轮廓 但也失败了 那个甚至不是圆的 div 的高度当前是静态的 但我希望它
  • Python 队列 get()/task_done() 问题

    我的消费者端队列 m queue get queue task done
  • 如何使用 pygame.mixer 重复音乐?

    我创建了以下使用 pygame mixer 播放 mp3 音乐的代码 然而 音乐不会重复 有什么想法可以让音乐重复播放吗 这是代码 playlist list playlist append put music here mp3 playl
  • 如何保持 python 3 脚本 (Bot) 运行

    不是母语英语 抱歉 英语可能很蹩脚 我也是编程新手 您好 我正在尝试使用 QueryServer 连接到 TeamSpeak 服务器来创建机器人 经过几天的努力 它有效 只有 1 个问题 而我却被这个问题困扰了 如果您需要检查 这是我正在使
  • 如何配置 Eclipse 在使用 Pydev 插件选择“运行”或“调试”时启动浏览器

    我正在使用 Eclipse Pydev 插件学习 Python 和 Django 当我按 运行 或 调试 时 我希望内部或外部浏览器使用 URL http 127 0 0 1 启动或刷新 我见过用 PHP 插件完成的 但没有用 Pydev

随机推荐