2

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 Secret Information 、Big Company Name (LBCN) カタログ番号 BCN2013R18 および BSSN 3-55564-789-Yは、SUP 2012 サムシング タスク フォースの一部として開発され、 <a href=" と併せて開催されました。 http://justaURL.com">*SEM 2013</a>、バナナ手榴弾とゴリラ戦術の研究に関する第 2 回合同会議 (Ape Warfare Studies) の利益団体 BUDDY HOLLY と LION KING。それは、モーガン・フリーマンと彼の信頼できる相棒ミシェル・バックマンの声で語られる 750 のグロス・ストーリーを含む 1 つの毛むくじゃらのオブジェクトで構成されています。

ただし、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 の使い方の問題ですか、それとも正規表現の問題ですか?

4

1 に答える 1

3

BeautifulSoup は、正規表現を使用して一致する要素を検索します。そのテキスト ノード全体が検索に一致します。

それでも、必要な部分を抽出する必要があります。BeautifulSoup はこれを行いません。ここで正規表現を再利用できます:

expression = re.compile('\,[\s\w()-]+\,')
textnode = HTML.body.p.find_all(text=expression)
print expression.search(textnode).group(0)
于 2013-10-24T17:29:50.653 に答える