11

我想获取 HTML 中隐藏输入字段的值。

<input type="hidden" name="fooId" value="12-3456789-1111111111" />

我想在 Python 中编写一个返回值的正则表达式fooId,因为我知道 HTML 中的行遵循格式

<input type="hidden" name="fooId" value="**[id is here]**" />

有人可以在 Python 中提供一个示例来解析 HTML 的值吗?

4

7 回答 7

27

对于这种特殊情况,BeautifulSoup 比正则表达式更难编写,但它更健壮......我只是贡献了 BeautifulSoup 示例,因为您已经知道要使用哪个正则表达式:-)

from BeautifulSoup import BeautifulSoup

#Or retrieve it from the web, etc. 
html_data = open('/yourwebsite/page.html','r').read()

#Create the soup object from the HTML data
soup = BeautifulSoup(html_data)
fooId = soup.find('input',name='fooId',type='hidden') #Find the proper tag
value = fooId.attrs[2][1] #The value of the third attribute of the desired tag 
                          #or index it directly via fooId['value']
于 2008-09-10T22:16:24.273 回答
18

我同意 Vinko BeautifulSoup是要走的路。但是我建议使用fooId['value']获取属性而不是依赖值作为第三个属性。

from BeautifulSoup import BeautifulSoup
#Or retrieve it from the web, etc.
html_data = open('/yourwebsite/page.html','r').read()
#Create the soup object from the HTML data
soup = BeautifulSoup(html_data)
fooId = soup.find('input',name='fooId',type='hidden') #Find the proper tag
value = fooId['value'] #The value attribute
于 2008-09-15T17:35:44.360 回答
8
import re
reg = re.compile('<input type="hidden" name="([^"]*)" value="<id>" />')
value = reg.search(inputHTML).group(1)
print 'Value is', value
于 2008-09-10T21:59:47.357 回答
5

解析是那些你真的不想自己动手的领域之一,如果你能避免的话,因为你将在未来几年内追逐边缘情况和错误

我建议使用BeautifulSoup。它有很好的声誉,从文档中看起来很容易使用。

于 2008-09-10T21:57:00.640 回答
1

Pyparsing 是 BeautifulSoup 和正则表达式之间的一个很好的过渡步骤。它比正则表达式更健壮,因为它的 HTML 标记解析理解大小写、空格、属性存在/不存在/顺序的变化,但比使用 BS 更简单地进行这种基本的标记提取。

您的示例特别简单,因为您要查找的所有内容都在打开的“输入”标签的属性中。这是一个 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]**

您可以看到,pyparsing 不仅匹配这些不可预测的变化,它还返回对象中的数据,从而可以轻松读取各个标签属性及其值。

于 2009-09-14T13:14:10.740 回答
0
/<input type="hidden" name="fooId" value="([\d-]+)" \/>/
于 2008-09-10T21:56:05.290 回答
0
/<input\s+type="hidden"\s+name="([A-Za-z0-9_]+)"\s+value="([A-Za-z0-9_\-]*)"\s*/>/

>>> import re
>>> s = '<input type="hidden" name="fooId" value="12-3456789-1111111111" />'
>>> re.match('<input\s+type="hidden"\s+name="([A-Za-z0-9_]+)"\s+value="([A-Za-z0-9_\-]*)"\s*/>', s).groups()
('fooId', '12-3456789-1111111111')
于 2008-09-11T09:41:15.970 回答