私はURLからタプルを抽出しようとしていると私はre.search(pattern_str, text_str)
を使用して文字列テキストとタプルを抽出することができました。しかし、私はre.findall(pattern_str, text_str)
を使ってタプルのリストを抽出しようとしたときに立ち往生しました。findallを使ってタプルを抽出するには?
テキストは次のようになります。
<li>
<a href="11111">
some text 111
<span class="some-class">
#11111
</span>
</a>
</li><li>
<a href="22222">
some text 222
<span class="some-class">
#22222
</span>
</a>
</li><li>
<a href="33333">
some text 333
<span class="some-class">
#33333
</span>
</a>
... # repeating
...
...
と私はタプルを抽出するために、次のパターン&コード使用しています:
text_above = "..." # this is the text above
pat_str = '<a href="(\d+)">\n(.+)\n<span class'
pat = re.compile(pat_str)
# following line is supposed to return the numbers from the 2nd line
# and the string from the 3rd line for each repeating sequence
list_of_tuples = re.findall(pat, text_above)
for t in list_of tuples:
# supposed to print "11111 -> blah blah 111"
print(t[0], '->', t[1])
は、たぶん私はその多分、不可能&奇妙な何かをしようとしているがプリミティブな文字列操作を使ってデータを抽出するほうがいいです...しかし、解決策がある場合は?
:あなただけのリストタグ内のアンカーをしたい場合は
また、あなたは、特にそれらを解析することができます。美しいスープのようなパーサーを使用してください! –