为什么不让我将列表转换为字符串?

时间:2019-01-01 04:33:05

标签: python beautifulsoup wikipedia

我正在使用BeautifulSoup抓取Wikipedia信息框,并尝试将其导出到表格中

我想将我的列表转换成BeatifulSoup,以便能够使用.find_all.find来查找嵌套标签,但是由于我没有在线找到任何要转换的标签,决定将其转换为字符串,然后尝试将其转换为漂亮的汤

当我尝试.join我的字符串时,出现错误:

  

TypeError:序列项0:预期的str实例,找到标签。

我也尝试过

print (u'').join(unicode(row1) for fow1 in link)
print (u'').join(row1.stripped_strings) 

但是这些会给出错误

  

AttributeError:'NoneType'对象没有属性'join'

my_table = soup.find('table',{'class':'infobox vcard'})

records = [] 

for my_tables in my_table:
    row1 = my_table.find_all('th',{'scope':'row'})
    print (row1)

    print()

    row2 = my_table.find_all('span')
    print (row2)

html = ''.join(row1)

它应该将列表转换为字符串

1 个答案:

答案 0 :(得分:0)

print不再是Python 3中的语句。它是一个函数。如果您使用from __future__ import print_function Try

,在Python 2中也是如此。
print(''.join(str(row1) for fow1 in link))