【问题标题】:Extracting a tag value in Beautiful Soup在 Beautiful Soup 中提取标签值
【发布时间】:2018-06-06 07:32:50
【问题描述】:

我在 python 中使用漂亮的汤来解析一个 html 文档。

我遇到了这样的标签

div class="_3auQ3N">\u20b9<!-- -->1,990</div>

\u20bp 代表货币符号,1,990 是价格。

我想知道如何将这些值提取到两个不同的字符串(或值)中?

【问题讨论】:

  • 到目前为止你尝试过什么?你有什么代码要显示吗?

标签: python html beautifulsoup


【解决方案1】:
>>> soup = BeautifulSoup('<div class="_3auQ3N">\u20b9<!-- -->1,990</div>', 'lxml')
>>> list(soup.div.strings)
['₹', '1,990']

【讨论】:

  • 我得到了这个.. [u'\\u20b9', u'1,990']
  • 对于 Python 2,您可以:for s in soup.div.strings: print s.decode('unicode_escape').
【解决方案2】:

一旦你提取了你的字符串,你就可以使用正则表达式:

import re


string = "\u20b9<!-- -->1,990"
a = re.findall("(^.*)<!-- -->(.*)", string)
print(a[0][0],a[0][1]) # ₹ 1,990

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多