2012-11-09 121 views
1

我用这个代码:如何分割多字符串数组

from bs4 import BeautifulSoup 

parser = BeautifulSoup(remote_data) 
parse_data = parser.find_all('a') 
for atag_data in parse_data: 
    URL_list = atag_data.get('href') 

当我尝试URL_LIST分成数组:

array = str.split(URL_list) 

我给这3个数组:

['index1.html'] 
['example.exe'] 
['document.doc'] 

但我只需要一个像这样的数组:

['index1.html','example.exe','document.doc'] 

有什么建议吗?

+2

请您输入的样品添加到您的问题。 – 2012-11-09 11:24:05

+0

你为什么试图再次分裂?它应该已经是一个列表(数组在Python中称为列表)。 – redShadow

+2

不管怎么说,要分割一个字符串,你要做''yourstring.split(separator)'',那''str.split(yourlist)''对我来说没什么意义.. – redShadow

回答

0

你没有得到一个数组 - 它是一个列表!另外,你应该避免像内建命名这样的变量。

关于你的问题:

from bs4 import BeautifulSoup 
parser = BeautifulSoup(remote_data) 
link_list = [a['href'] for a in parser.find_all('a')] 
+0

非常感谢, ! =) –

+0

你是什么意思'你应该避免命名像builtins'这样的变量?在他的代码中我没有看到任何这种情况。 – Matt

+0

@Matt:你不应该命名变量“array”,“dict”,“list”等。我的意思是'array = str.split(URL_list)'这一行。 – sphere