如何使用python常规制作宽度和高度x2 [英] how to make the width and height x2 using python Regular
本文介绍了如何使用python常规制作宽度和高度x2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须做很多工作才能像这样改变:
到
所以我想使用 python 常规这是我的代码:
导入重新s = '<img src = "werwerwe" height="111" width="10"/>'定义 a(x):打印 x.group(2)打印 x.group(4)ss = re.sub(r'''<img.*(width\s*="?(\d+)"?)*\s*(height\s*="?(\d+)"?)*''',作为)打印 SS
那我能怎么办,
谢谢
更新:
现在可以了:
导入重新s = '<img src = "/" height="111" width="10"/>'定义 a(x):b = x.group(0)b = b.replace(x.group(1),str(int(x.group(1))*2))b = b.replace(x.group(2),str(int(x.group(2))*2))返回 bss = re.sub(r''']*>''',作为)打印 SS
解决方案
不要使用正则表达式来解析 HTML.使用 BeautifulSoup
<预><代码>>>>从 BeautifulSoup 导入 BeautifulSoup>>>ht = '<html><head><title>foo</title></head><body><p>whatever: <img src="foo/img.png" height="111" width="22"/></p><ul><li><img src="foo/img2.png" height="32" width="44"/</li></ul></body></html>'>>>汤 = BeautifulSoup(ht)>>>汤<html><head><title>foo</title></head><body><p>whatever: <img src="foo/img.png" height="111" width="22"/></p><ul><li><img src="foo/img2.png" height="32" width="44"/></li></ul></body></html>>>>汤.findAll('img')[<img src="foo/img.png" height="111" width="22"/>, <img src="foo/img2.png" height="32" width="44"/>]>>>对于soup.findAll('img')中的img:... ht = int(img['height'])... wi = int(img['width'])... img['height'] = str(ht * 2)... img['width'] = str(wi * 2)......>>>打印汤.美化()<头><标题>富头部><身体><p>任何:<img src="foo/img.png" height="222" width="44"/></p><ul><li><img src="foo/img2.png" height="64" width="88"/></html>>>>i have to do many work to change like this:
<img src = "/" height="111" width="10" />
to
<img src = "/" height="222" width="20" />
so i want to use python Regular this is my code :
import re
s = '<img src = "werwerwe" height="111" width="10" />'
def a(x):
print x.group(2)
print x.group(4)
ss = re.sub(r'''<img.*(width\s*="?(\d+)"?)*\s*(height\s*="?(\d+)"?)*''',a, s)
print ss
so what can i do ,
thanks
updated:
it is ok now :
import re
s = '<img src = "/" height="111" width="10" />'
def a(x):
b = x.group(0)
b = b.replace(x.group(1),str(int(x.group(1))*2))
b = b.replace(x.group(2),str(int(x.group(2))*2))
return b
ss = re.sub(r'''<img.*?height=\"(\d+)\".*?width=\"(\d+)\"[^>]*>''',a, s)
print ss
解决方案
Don't use regular expressions to parse HTML. Use BeautifulSoup
>>> from BeautifulSoup import BeautifulSoup
>>> ht = '<html><head><title>foo</title></head><body><p>whatever: <img src="foo/img.png" height="111" width="22" /></p><ul><li><img src="foo/img2.png" height="32" width="44" /></li></ul></body></html>'
>>> soup = BeautifulSoup(ht)
>>> soup
<html><head><title>foo</title></head><body><p>whatever: <img src="foo/img.png" height="111" width="22" /></p><ul><li><img src="foo/img2.png" height="32" width="44" /></li></ul></body></html>
>>> soup.findAll('img')
[<img src="foo/img.png" height="111" width="22" />, <img src="foo/img2.png" height="32" width="44" />]
>>> for img in soup.findAll('img'):
... ht = int(img['height'])
... wi = int(img['width'])
... img['height'] = str(ht * 2)
... img['width'] = str(wi * 2)
...
...
>>> print soup.prettify()
<html>
<head>
<title>
foo
</title>
</head>
<body>
<p>
whatever:
<img src="foo/img.png" height="222" width="44" />
</p>
<ul>
<li>
<img src="foo/img2.png" height="64" width="88" />
</li>
</ul>
</body>
</html>
>>>
这篇关于如何使用python常规制作宽度和高度x2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文