关于regex:用于HTML解析的Python正则表达式(BeautifulSoup)

关于regex:用于HTML解析的Python正则表达式(BeautifulSoup)

Python regular expression for HTML parsing (BeautifulSoup)

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

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

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

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

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


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

1
2
3
4
5
6
7
8
9
10
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']

我同意Vinko BeautifulSoup是必经之路。但是我建议使用fooId['value']来获取属性,而不是依赖于值作为第三个属性。

1
2
3
4
5
6
7
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

1
2
3
4
import re
reg = re.compile('<input type="hidden" name="([^"]*)" value="<id>" />')
value = reg.search(inputHTML).group(1)
print 'Value is', value


解析是您真正不想自己避免的领域之一,因为您将追逐边缘情况,并且错误会持续很多年

我建议您使用BeautifulSoup。它具有很高的声誉,并且从文档中看起来非常易于使用。


Pyparsing是BeautifulSoup和regex之间的一个很好的过渡步骤。它比正则表达式更强大,因为它的HTML标签解析可以理解大小写,空格,属性存在/不存在/顺序的变化,但是比起使用BS,进行这种基本标签提取更容易。

您的示例特别简单,因为您要查找的所有内容都位于打开的" input"标记的属性中。这是一个pyparsing示例,显示了输入标签上的几种变体,这些变体将使正则表达式适合,并且还显示了如何在标记中添加不匹配的标记:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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

打印:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
['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不仅匹配了这些不可预测的变化,而且还返回了对象中的数据,从而可以轻松地读取各个标签属性及其值。


1
2
3
4
5
6
/<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')

1
/<input type="hidden" name="fooId" value="([\\d-]+)" \\/>/

推荐阅读

    linux输入过的命令?

    linux输入过的命令?,系统,地址,数字,命令,工具,工作,环境,界面,历史,指令,lin

    编程解析linux命令?

    编程解析linux命令?,系统,标准,基础,设备,发行,电脑,工具,密码,名字,适当,如

    linux命令输入在那?

    linux命令输入在那?,系统,工具,电脑,命令,终端,发行,工作,管理,密码,下面,lin

    linux输入文本命令?

    linux输入文本命令?,系统,位置,电脑,工作,首开,命令,终端,模式,指令,字符,如

    手机输入linux命令?

    手机输入linux命令?,系统,手机,软件,电脑,设备,管理,工作,命令,领导,第一,lin

    linux文件输入命令?

    linux文件输入命令?,工作,系统,地址,信息,工具,位置,命令,设备,发行,首开,lin

    linux命令行不能输入?

    linux命令行不能输入?,工作,系统,电脑,服务,命令,名字,首次,百度,管理,第一,l

    linux中命令如何输入?

    linux中命令如何输入?,系统,电脑,地址,工具,发行,命令,终端,密码,名字,网站,l

    linux下抓取字段命令?

    linux下抓取字段命令?,数据,系统,命令,单位,报告,工具,字符串,文件,范本,样

    linux正向解析命令?

    linux正向解析命令?,系统,工作,地址,命令,管理,单位,信息,数据,目录,常用命

    linux输入命令变乱码?

    linux输入命令变乱码?,系统,统一,乱码,地方,情况,数据,中文,字符集,服务器,

    linux快捷输入命令?

    linux快捷输入命令?,系统,位置,命令,终端,名称,首页,分行,第一,快捷键,窗口,l

    linux输入命令的步骤?

    linux输入命令的步骤?,系统,工作,命令,发行,第三,数字,时间,管理,首开,基础,l

    linux命令输入ip?

    linux命令输入ip?,地址,系统,网络,代码,设备,密码,信息,电脑,数字,名称,linux

    linux打命令输入密码?

    linux打命令输入密码?,密码,系统,状态,代码,管理,标的,位置,地址,名称,命令,L

    linux提取字段串命令?

    linux提取字段串命令?,数字,字符串,状态,工具,命令,文件,范本,样式,正则,字

    linux输入命令后乱码?

    linux输入命令后乱码?,系统,乱码,中文,情况,环境,地方,名称,字符集,服务器,

    linux输入命令无反应?

    linux输入命令无反应?,密码,系统,数据,命令,工具,时报,信息,检测,文件,反应,L

    linux命令行输入引号?

    linux命令行输入引号?,系统,密码,命令,电脑,标准,地方,环境,管理,名字,表示,

    怎么输入linux命令?

    怎么输入linux命令?,系统,工具,命令,电脑,工作,终端,数字,时间,软件,分行,lin