很多空格 beautifulsoup

A lot of whitespace beautifulsoup

我正在使用 beautifulsoup 进行网络抓取。该网页有以下来源:

<td>\n<a href="http://aaa.com">Charles</a>\r\n                         (hello)\r\n                            </td>,
<td>\n<a href="http://bbb.com">Diane</a>\r\n                           (hi)\r\n                            </td>,
<td>\n<a href="http://ccc.com">Kevin</a>\r\n                           (how are you doing)\r\n                            </td>

我使用以下代码打印两个值。他们工作得很好。

for item in soup.find_all("td"):
    print item.find('a').text
    print item.find('a').next_sibling

问题是当我将输出保存在 csv 文件中时,第二列没有值。出现是因为有很多空格。有什么建议吗?提前致谢。

找到所有 next text siblings,加入它们并剥离:

"".join(item.find('a').find_next_siblings(text=True)).strip()