【问题标题】:A lot of whitespace beautifulsoup很多空白美丽的汤
【发布时间】:2015-12-03 07:12:43
【问题描述】:

我正在使用 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 文件中时,第二列没有任何值。出现是因为有很多空白。有什么建议吗?提前致谢。

【问题讨论】:

    标签: python python-2.7 beautifulsoup html-parsing


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多