-3

私はこのようなコードを書きました:

print re.findall(r'(<td width="[0-9]+[%]?" align="(.+)">|<td align="(.+)"> width="[0-9]+[%]?")([ \n\t\r]*)([0-9,]+\.[0-9]+)([ \n\t\r]*)([&]?[a-zA-Z]+[;]?)([ \n\t\r]*)<span class="(.+)">',r.text,re.MULTILINE)

この行を取得するには:

<td width="47%" align="left">556.348&nbsp;<span class="uccResCde">

値556.348が必要です。正規表現を使用して取得するにはどうすればよいですか?

4

2 に答える 2

3

HTMLParser のドキュメントから直接カット アンド ペーストすると、正規表現を使用せずにタグからデータを取得できます。

from HTMLParser import HTMLParser

# Create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print "Encountered a start tag:", tag
    def handle_endtag(self, tag):
        print "Encountered an end tag :", tag
    def handle_data(self, data):
        print "Encountered some data  :", data

# Instantiate the parser and fed it some HTML
parser = MyHTMLParser()
parser.feed('<td width="47%" align="left">556.348&nbsp;<span class="uccResCde">')
于 2012-08-30T13:24:14.647 に答える
0

これは、一致したグループを取得する方法を説明するソリューションです。ドキュメントを読む必要があります。

import re

text_to_parse= '<td width="47%" align="left">556.348&nbsp;<span class="uccResCde">'
pattern = r'(<td width="[0-9]+[%]?" align="(.+)">|<td align="(.+)"> width="[0-9]+[%]?")([ \n\t\r]*)([0-9,]+\.[0-9]+)([ \n\t\r]*)([&]?[a-zA-Z]+[;]?)([ \n\t\r]*)<span class="(.+)">'
m = re.search(pattern, text_to_parse)
m.group(5)

しかし、HTML を解析するために正規表現を使用する必要はありません。代わりに、 Beautiful Soupなどの HTML パーサーを使用します。

from bs4 import BeautifulSoup

soup = BeautifulSoup(text_to_parse)
soup.text
于 2012-08-30T13:12:40.150 に答える