在Python Django中用另一个标记替换标记
我正在使用Python,我有一些问题:在Python Django中用另一个标记替换标记,python,replace,beautifulsoup,Python,Replace,Beautifulsoup,我正在使用Python,我有一些问题: 我正在使用BeautifulSoup我想用另一个HTML标记替换一个HTML标记 下面是代码示例: html = BeautifulSoup(p) x = html.find('a', attrs={'href':'/slideshow'}) while x: print 'x unchanged - ', x x=x.replaceWith(
html = BeautifulSoup(p)
x = html.find('a', attrs={'href':'/slideshow'})
while x:
print 'x unchanged - ', x
x=x.replaceWith('<a href="/slideshow_v2">')
print 'x changed - ', x
html=BeautifulSoup(p)
x=html.find('a',attrs={'href':'/slideshow'})
而x:
打印“x未更改-”,x
x=x。替换为(“”)
打印“x已更改-”,x
谢谢你的帮助 以下是您的问题的解决方案:
html = BeautifulSoup(p)
anchors = html.findAll('a', href='/slideshow')
for anchor in anchors:
anchor['href'] = '/slideshow_v2'
print html.findAll('a', href='/slideshow_v2')
请考虑到这不是一个与Django有关的问题
祝你好运