开发者

Replace tag with another in Python Django

开发者 https://www.devze.com 2023-02-28 09:58 出处:网络
I have working with Python and I have some questions: I am using BeautifulSoup I want to replace an HTML tag with another one.

I have working with Python and I have some questions:

  1. I am using BeautifulSoup I want to replace an HTML tag with another one.

Here is code example:

html = BeautifulSoup(p)
            x = html.find('a', attrs={'href':'/slideshow'})

            while x:
                print 'x unchanged - ', x
                x=x.replaceWith('<a href="/slideshow_v开发者_StackOverflow2">')
                print 'x changed - ', x

Thanks for help !!!


here is a solution to your problem:

html = BeautifulSoup(p)
anchors = html.findAll('a', href='/slideshow')

for anchor in anchors:
    anchor['href'] = '/slideshow_v2'

print html.findAll('a', href='/slideshow_v2')

Take into acount please that this is not a Django related question.

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号