python 如何获取html<area>里面的值?
<area shape="rect" coords="157,804,323,848" href="#" />,怎么获取属性coords的4个值?以达到:“A=157 B=804 C=323 D=848”这样的效果。
<area shape="rect" coords="157,804,323,848" href="#" />,怎么获取属性coords的4个值?以达到:“A=157 B=804 C=323 D=848”这样的效果。
答案:两种方法。第一种用正则:
import re
a = '<area shape="rect" coords="157,804,323,848" href="#" />'
b = re.compile('rect" coords="(.*?)" href="#" ')
c = b.findall(a)[0]
d = c.split(",")
A = d[0]
B = d[1]
C = d[2]
D = d[3]
print A,B,C,D
第二种用切片
a = '<area shape="rect" coords="157,804,323,848" href="#" />'
b = a.find('rect" coords="')
c = a.find('" href="#" ')
d = a[b+len('rect" coords="'):c]
e = d.split(",")
A = e[0]
B = e[1]
C = e[2]
D = e[3]
print A,B,C,D
好了
其他:用正则表达式分割就可以
import re
s = '<area shape="rect" coords="157,804,323,848" href="#" />'
ptn = re.compile(".+\"(\d+),(\d+),(\d+),(\d+)\".+") #正则表达式可以根据要求再细微化
result = ptn.match(s)
A = int(result.group(1))
B = int(result.group(2))
C = int(result.group(3))
D = int(result.group(4))
=============================================================
或者:
import re
s = '<area shape="rect" coords="157,804,323,848" href="#" />'
ptn = re.compile(".+coords=\"([\d,]+)\".+") #正则表达式可以根据要求再细微化
result = ptn.match(s)
arr = result.group(1).split(",")
这时arr列表里面存放的就是分开的四个字符串,转化成整型赋值即可。 不知道你这是什么玩意啊
我给你一个在 jquery下操作html的思路:
//获得属性值
var attrValue = $("area").attr("coords");
//然后切割字符串
array alist = new array();
alist = attrValue.split(",");
alist[0] alist[1] alist[2] alist[3] 就是你所需要的几个值
你在拼接一下就行了
比如:
var str = "A="+alist[0]+" B="+alist[1]]+" C="+alist[2]]+" D="+alist[3] 通过模板继承关系
$shape = varShap
$coords = varCoords
$href = varHref
------------------------------
A = parent.coords.split(",")[0]
B = parent.coords.split(",")[1]
上一个:如何使jquery动态改变页面之后,原html页面中的代码也改变并保存了下来
下一个:HTML中怎么判断文本框的值,当是一个值时点击就清空,否则不清空