【问题标题】:Searching a variable pattern using Python使用 Python 搜索变量模式
【发布时间】:2016-01-28 16:51:14
【问题描述】:

如何从以下 HTML 代码中找出变量模式?

<element aria-label="like this video along with 3,358,980 other people" >...<element>

我想从上面的行中找到号码。

【问题讨论】:

  • html 代码?????/我没有找到任何标签..
  • @AvinashRaj:我猜是从一些input 元素中提取的。
  • 这已经被问了一千次了,我们在哪里可以看到你的一些努力?请记住:这不是一个倾倒您的待办事项列表的网站。

标签: python html regex


【解决方案1】:

在这种情况下,我认为您需要一个简单的正则表达式。只需查找一串数字和可选的逗号:

>>> s='aria-label="like this video along with **3,358,980** other people"'
>>> print(re.search(r"([\d,]+)", s).group(0))
3,358,980

【讨论】:

    【解决方案2】:

    如果感兴趣的行始终是“some text **number_of_interest** some more text”,那么您可以将字符串拆分为双星号 **。

    split_string = "this is the number **3,444,333** I need".split('**')
    number_of_interest = split_string[1]
    

    【讨论】:

      【解决方案3】:

      这仅适用于具有可选值的整数、非小数小数
      千分隔符。

      \d+(?:,\d{3})*

      【讨论】:

        猜你喜欢
        • 2016-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-07
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多