Reputation: 403
Here is my .xml file:
<?xml version="1.0" encoding="US-ASCII"?>
<virtualhosts>
<virtualhost id="113">
<name>example.com</name>
<hostname>myexample.com</hostname>
</virtualhost>
</virtualhosts>
</xml>
Now I want to extract the value of the id attribute of the virtualHost
tag (in that case113
) like described here. Here is my code:
import sys
from bs4 import BeautifulSoup as Soup
file ="temp.xml"
handler = open(file).read()
soup = Soup(handler,features="lxml")
for vHost in soup.find('virtualhost').find_all('name', string='example.com'):
print(vHost.parent)#display the right level
temp=vHost.parent.virtualhost['id']
print(temp)
But I get the following error:
TypeError: 'NoneType' object is not subscriptable
Can someone help me?
Upvotes: 1
Views: 235
Reputation: 33384
Since you have identified the parent just print this.
for vHost in soup.find('virtualhost').find_all('name', string='example.com'):
print(vHost.parent)#display the right level
temp=vHost.parent['id']
print(temp)
Upvotes: 0
Reputation: 1377
You can use attrs function
Use contex manager so you don't have to remember about closing files.
import sys
from bs4 import BeautifulSoup as Soup
file ="temp.xml"
with open(file) as f:
handler = f.read()
soup = Soup(handler,features="lxml")
all_names = soup.find('virtualhost').find_all('name', string='example.com')
for vHost in all_names:
print(vHost.parent)#display the right level
temp=vHost.parent.attrs['id']
print(temp)
Upvotes: 1