gpt4 book ai didi

python - 在 BeautifulSoup 中匹配逗号

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:38 24 4
gpt4 key购买 nike

我用 Pythex 测试了我的正则表达式它按预期工作:

HTML:

Something Very Important (SVI) 2013 Sercret Information, Big Company
Name (LBCN) Catalog Number BCN2013R18 and BSSN 3-55564-789-Y, was
developed as part of the SUP 2012 Something Task force was held in
conjunction with <a href="http://justaURL.com">*SEM 2013</a>, the second joint conference on study of
banana hand grenades and gorilla tactics (Association of Ape Warfare
Studies) interest groups BUDDY HOLLY and LION KING. It is comprised of
one hairy object containing 750 gross stories told in the voice of
Morgan Freeman and his trusty sidekick Michelle Bachman.

我的正则表达式:

,[\s\w()-]+,

Pythex 一起使用时它选择了我正在寻找的区域,该区域位于段落中的 2 个逗号之间:

Something Very Important (SVI) 2013 Sercret Information , Big Company Name (LBCN) Catalog Number BCN2013R18 and BSSN 3-55564-789-Y, was developed as part of the SUP 2012 Something Task force was held in conjunction with <a href="http://justaURL.com">*SEM 2013</a>, the second joint conference on study of banana hand grenades and gorilla tactics (Association of Ape Warfare Studies) interest groups BUDDY HOLLY and LION KING. It is comprised of one hairy object containing 750 gross stories told in the voice of Morgan Freeman and his trusty sidekick Michelle Bachman.

但是当我使用 BeautifulSoup 的文本正则表达式时:

print HTML.body.p.find_all(text=re.compile('\,[\s\w()-]+\,'))

我返回的是这个而不是逗号之间的区域:

[u'Something Very Important (SVI) 2013 Sercret Information, Big Company Name (LBCN) Catalog Number BCN2013R18 and BSSN 3-55564-789-Y, was developed as part of the SUP 2012 Something Task force was held in conjunction with ']

我也试过转义逗号,但没有成功。美汤只想退全<p>而不是我指定的正则表达式。我还注意到它返回段落直到中间的那个链接。这是我使用 BeautifulSoup 的问题还是正则表达式问题?

最佳答案

BeautifulSoup 使用正则表达式来搜索匹配的元素。整个文本节点与您的搜索匹配。

你仍然需要提取你想要的部分; BeautifulSoup 不会为你做这些。您可以在这里重用您的正则表达式:

expression = re.compile('\,[\s\w()-]+\,')
textnode = HTML.body.p.find_all(text=expression)
print expression.search(textnode).group(0)

关于python - 在 BeautifulSoup 中匹配逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19572573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com