navigating complex xml tags beautiful soup code example

Example 1: how to convert response to beautifulsoup object

import requests
from bs4 import BeautifulSoup

url = 'https://www.google.com'

response = requests.get(url)

html_file = BeautifulSoup(response.text, "html.parser")

print(html_file.prettify())

Example 2: tag inside tag beautifulsoup

html = """<div class="pr">
</div>
<li>
 <a href="pr/protocol">
  protocol
 </a>
 </li>"""

soup = BeautifulSoup(html, "lxml")

a = soup.select_one("div[class=pr]")

# Li parent
parent = soup.new_tag("li", class_="parent")
# Child anchor
child = soup.new_tag("a", href="hm/test", class_="child")
child.string = 'TEST'
# Append child to parent
parent.append(child)
# Insert parent
a.insert_after(parent)
print(soup.prettify())