- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的 html 看起来像:
<td>
<table ..>
<tr>
<th ..>price</th>
<th>$99.99</th>
</tr>
</table>
</td>
所以我在当前表格单元格中,如何获得 99.99 值?
我到目前为止:
td[3].findChild('th')
但我需要做的是:
找到带有文本 'price' 的 th,然后获取下一个标签的字符串值。
最佳答案
在“步骤”中考虑它...假定某些 x
是您正在考虑的子树的根,
x.findAll(text='price')
是包含文本 'price'
的子树中所有项目的列表。这些项目的 parent 当然是:
[t.parent for t in x.findAll(text='price')]
如果你只想保留那些“名字”(标签)是'th'
的,那当然
[t.parent for t in x.findAll(text='price') if t.parent.name=='th']
并且您想要那些的“下一个 sibling ”(但前提是他们也是'th'
),所以
[t.parent.nextSibling for t in x.findAll(text='price')
if t.parent.name=='th' and t.parent.nextSibling and t.parent.nextSibling.name=='th']
在这里您可以看到使用列表理解的问题:重复太多,因为我们无法将中间结果分配给简单的名称。因此,让我们切换到一个很好的旧循环......:
编辑:添加了对父 th
和“下一个兄弟”之间的文本字符串的容忍度,以及对后者是 td< 的容忍度
相反,根据 OP 的评论。
for t in x.findAll(text='price'):
p = t.parent
if p.name != 'th': continue
ns = p.nextSibling
if ns and not ns.name: ns = ns.nextSibling
if not ns or ns.name not in ('td', 'th'): continue
print ns.string
我已经添加了 ns.string
,当且仅当它们只是文本(没有进一步的嵌套标签)时,它将给出下一个兄弟的内容——当然你可以进一步分析这一点,取决于你的应用程序的需要!-)。同样,我想你不会只是做 print
,而是做一些更聪明的事情,但我给你的是结构。
谈到结构,请注意我两次使用 if...: continue
:与反转 if
的条件和缩进的替代方案相比,这减少了嵌套循环中的所有以下语句——“扁平优于嵌套”是 Python 之禅中的一个公案(import this
在交互式提示下查看所有内容并进行冥想;-) .
关于python - beautifulsoup,找到文本为 'price' 的第一个,然后从下一个开始获取价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3376803/
我想创建一个 sql 查询,为 2 个不同的查询一起返回结果。例如,我想要以下形式的结果:产品名称, avg(price), min(price), max(price), avg(order), m
我找不到 itemprop s 表示 schema.org 上有两种价格的产品。 价格类型有: 旧价 全新 Markdown 我无法想象,没有适合的 schema.org 标记,但经过长时间的搜索,我
我正在尝试更改 Magento 中的“特价”和“正常价格”措辞。我已经尝试编辑 Mage_Catalogue.csv、重新上传并清除缓存,但无济于事。我还在整个站点内对“常规”和“特殊”文本进行了巨大
考虑下表: sp100_id _date open close bullishness ----------------------------------------------
我试图显示某些产品变体的价格比较,但我不知道如何仅在大于零的变体的价格比较时显示它。它显示的是 0.00 美元价格的比较。 它使用 JavaScript 来动态更新价格。这是代码: 当我有一个原
我需要一些帮助来在 xml 中构建这一行。对于谷歌内容 API 我想要得到的结果是 25 目前我的代码输出这一行 25这是不正确的。 这是我的代码 b = Nokogiri::XML::Builder
我想在产品详细信息页面的价格框中显示带有名称的自定义选项价格。 我也试过这个链接但没有成功this is link i use 所以请给我建议任何解决方案。 最佳答案 首先你必须按下按钮 calcul
我想除以 {{ item.price |没有货币的货币 }} 提高 1.21。我正在隐藏输入值中调用 item.price。 When I do the below, it shows the va
因此,我想验证用户在文本字段中输入的价格: 条件(或限制): 值应始终大于零 值不能包含负号“-” 值不能多次包含小数点。还值不应以小数点开头 此外,我必须记住,这也应该适用于移动设备,因为该应用程
我使用分级价格,并对 1 到 4 之间的数量设定了固定价格,对超过 4 的数量设定了每件商品的价格。 我想修改我的购物车,为数量 >= 1 && 数量 <=4 的商品设置固定价格 我该怎么做? 这是我
Google In-app payment API 中有没有办法从应用中设置商品的价格? 我想在我的应用程序中创建一个捐赠我的应用程序按钮。会有一些预定义的捐赠金额(例如 1 美元、5 美元、10 美
我有一个数据库,其中有一列名为“价格” - 在此列中,价格四舍五入到小数点后 5 位,例如 62.083333 - 我想运行命令将“price”列中的每个值更改为仅 2 位小数,例如 62.08 是否
我的数据库中有一个名为“价格”的字段,它设置为 varchar。它包含美元符号和逗号。 我的数据库中的值是这样的: 100,000 美元 625,005 美元 115,990 美元 2,450,000
我有一个基本的 html 表单,它使用 PHP 向页面添加一些数据。我想使这个表单字段成为“价格”字段。我的 HTML 是: 如何修复此字段,以便用户无法输入随机字母、美元符号、破折号或其他奇怪的格
我试过这段代码,但没有给出解决方案。不使用引导表。我如何管理每项服务的点数。价格如何出现在适当的列中以及点数如何自动管理。 Title Rates
它们是一种将字符串格式化为 $ 价格的内置方式,例如12345.45 转换为 $12,345.45? 最佳答案 假设您使用的是 Cocoa(或只是 Foundation),您可以使用 NSNumber
我正在尝试获取 UniswapV2 上一对的价格:这是我的代码: pragma solidity ^0.5.1; import '@uniswap/v2-core/contracts/interfac
我正在寻找一种替代方法 template/catalog/product/price.phml在一个特定位置使用,并继续在所有其他位置使用现有的 price.phtml 文件。 为了进一步解释,我需要
我正在使用 VBScript(经典 ASP)和 SQL Server;我正在尝试在网站上设置一个部分,您可以在其中查看特定价格水平的产品数量。像这样的东西: $50 - $100 (4) $100 -
鉴于以下价格列表,我试图弄清楚如何仅规范化/提取数字。 INPUT DESIRED_OUTPUT CA$1399.00 1399.00 $1399.11 1399
我是一名优秀的程序员,十分优秀!