<project id=""><name>Main account</name><balance>1236.59</balance><htmlchar>$</htmlchar><currency>HKD</currency><balancem>1236.59</balancem></project>
我想使用漂亮的汤从Main account
中提取值name
。我知道我可以使用balance
从foo.balance.string
中提取值,但似乎不适用于name
。
答案 0 :(得分:3)
from bs4 import BeautifulSoup
soup = BeautifulSoup("""<project id=""><name>Main account</name><balance>1236.59</balance><htmlchar>$</htmlchar><currency>HKD</currency><balancem>1236.59</balancem></project>""")
print soup.find("name").string
结果:
Main account
答案 1 :(得分:1)
希望这会对你有所帮助。
html_doc = "<project id=''><name>Main account</name><balance>1236.59</balance><htmlchar>$</htmlchar><currency>HKD</currency><balancem>1236.59</balancem></project>"
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
print soup.find('name').string