2

我有一个这样的xml:

<link>
www.link1.com
</link>
<link>
www.link2.com
</link>

我试过这段代码:

from BeautifulSoup import BeautifulStoneSoup 
soup = BeautifulStoneSoup(results2)     #Beautiful Soup
linklist = soup.findAll('link')
print soup

使用此代码,输出为

[<link>www.link1.com</link>,<link>www.link2.com</link>]

但我想要这样的输出

[www.link1.com, www.link2.com]
4

3 回答 3

8

你有没有尝试过:

linklist = [el.string for el in soup.findAll('link')]
于 2012-07-12T15:05:55.250 回答
1
links = soup.find_all('link')
link_strings = [s.string for s in links.string]
于 2012-07-12T15:13:50.283 回答
1

尝试这个:

from bs4 import BeautifulSoup

xml = """<html><link>
www.link1.com
</link>
<link>
www.link2.com
</link></html>"""

soup = BeautifulSoup(xml,features="xml")
linklist = soup.find_all('link')
linklist = map(lambda x: x.string, linklist)

请注意,我已将构造函数更改为BeautifulSoupwithfeatures="xml"而不是BeautifulStoneSoup因为后者已被弃用。

于 2012-07-12T15:16:00.537 回答