PyparsingはBeautifulSoupと正規表現の中間段階です。 HTMLタグ解析では、大文字と小文字、空白、属性の有無、順序の違いが分かりますが、BSを使うよりも基本タグ抽出の方が簡単なので、正規表現だけではありません。
あなたの探しているものはすべて、開始の "input"タグの属性にあるので、あなたの例は特に簡単です。ここでは正規表現のフィットを与えるだろう、あなたのinputタグにはいくつかのバリエーションを示すpyparsing例があり、また、それはコメント内であれば、タグに一致するようにしない方法を示しています。
html = """<html><body>
<input type="hidden" name="fooId" value="**[id is here]**" />
<blah>
<input name="fooId" type="hidden" value="**[id is here too]**" />
<input NAME="fooId" type="hidden" value="**[id is HERE too]**" />
<INPUT NAME="fooId" type="hidden" value="**[and id is even here TOO]**" />
<!--
<input type="hidden" name="fooId" value="**[don't report this id]**" />
-->
<foo>
</body></html>"""
from pyparsing import makeHTMLTags, withAttribute, htmlComment
# use makeHTMLTags to create tag expression - makeHTMLTags returns expressions for
# opening and closing tags, we're only interested in the opening tag
inputTag = makeHTMLTags("input")[0]
# only want input tags with special attributes
inputTag.setParseAction(withAttribute(type="hidden", name="fooId"))
# don't report tags that are commented out
inputTag.ignore(htmlComment)
# use searchString to skip through the input
foundTags = inputTag.searchString(html)
# dump out first result to show all returned tags and attributes
print foundTags[0].dump()
print
# print out the value attribute for all matched tags
for inpTag in foundTags:
print inpTag.value
プリント:
['input', ['type', 'hidden'], ['name', 'fooId'], ['value', '**[id is here]**'], True]
- empty: True
- name: fooId
- startInput: ['input', ['type', 'hidden'], ['name', 'fooId'], ['value', '**[id is here]**'], True]
- empty: True
- name: fooId
- type: hidden
- value: **[id is here]**
- type: hidden
- value: **[id is here]**
**[id is here]**
**[id is here too]**
**[id is HERE too]**
**[and id is even here TOO]**
これらの予期しないバリエーションにピーパルが一致するだけでなく、個々のタグ属性とその値を簡単に読み取ることができるオブジェクト内のデータが返されることがわかります。
「新しい」キーワードは不一致だと私は思っています。 –