我在使用漂亮的汤在xml中找到<name> </name>时遇到了问题

时间:2014-06-28 18:17:27

标签: xml beautifulsoup

我正在尝试使用beautifulsoup.name获得美丽的汤返回文本。但它只是给了我[文件]一个美丽的汤对象。关于如何使用Beautiful Soup在XML文件中返回文本的任何建议?

import BeautifulSoup as bsoup
f = open(file)
soup = bsoup(f)
f.close()

f = soup.name

This code will return [document]


But I'm looking to for a function in beautiful soup to return:

<name>car</name>

1 个答案:

答案 0 :(得分:0)

根据Beautiful Soup 4的Beautiful Soup Documentation

我看到了

你想要

import BeautifulSoup as bsoup
f = open(file)
soup = BeautifulSoup(f)
f.close()`
print(soup.find('name'))

打印<name>car</name>print (soup.find('name').name)以打印car