How can I insert a new tag into a BeautifulSoup object?

前端 未结 3 2084
小鲜肉
小鲜肉 2020-11-30 08:39

Trying to get my head around html construction with BS.

I\'m trying to insert a new tag:

self.new_soup.body.insert(3, \"\"\"
3条回答
  •  天命终不由人
    2020-11-30 08:57

    See the documentation on how to append a tag:

    soup = BeautifulSoup("")
    original_tag = soup.b
    
    new_tag = soup.new_tag("a", href="http://www.example.com")
    original_tag.append(new_tag)
    original_tag
    # 
    
    new_tag.string = "Link text."
    original_tag
    # Link text.
    

提交回复
热议问题