当我想要一列时,Python to CSV 将字符串分成两列

str*_*gap 5 python csv beautifulsoup

我正在用 BeautifulSoup 抓取一个页面,部分逻辑是有时<td>标签的部分内容可以包含<br>在其中。

所以有时它看起来像这样:

<td class="xyz">
    text 1
    <br>
    text 2
</td>
Run Code Online (Sandbox Code Playgroud)

有时它看起来像这样:

<td class="xyz">
    text 1
</td>
Run Code Online (Sandbox Code Playgroud)

我正在遍历这个并添加到一个 output_row 列表中,我最终将它添加到一个列表列表中。无论我看到前一种格式还是后一种格式,我都希望文本位于一个单元格中。

我找到了一种方法来确定我是否看到了<br>标签,因为 td.string 显示为 none 并且我还知道文本 2 中总是包含“ABC”。所以:

    elif td.string == None:
        if 'ABC' in td.contents[2]:
            new_string = td.contents[0] + ' ' + td.contents[2]
            output_row.append(new_string)
            print(new_string)
        else:    
            #this is for another situation and it works fine
Run Code Online (Sandbox Code Playgroud)

当我在 Jupyter Notebook 中打印它时,它显示为“text 1 text 2”作为一行。但是当我打开我的 CSV 时,它在两个不同的列中。因此,当 td.string 有内容(意味着没有<br>标签)时,文本 1 会显示在一列中,但是当我找到带有<br>标签的部分时,我的所有数据都会发生变化。

我不确定为什么在将它们附加到列表之前将它们连接起来时它显示为两个不同的字符串(两列)。

我正在写这样的文件:

with open('C:/location/file.csv', 'w',newline='') as csv_file:
    writer=csv.writer(csv_file,delimiter=',')
    #writer.writerow(headers)
    for row in output_rows:
        writer.writerow(row)

csv_file.close
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 2

get_text()您可以使用“strip”和“separator”来处理这两种情况:

from bs4 import BeautifulSoup

dat="""
<table>
    <tr>
        <td class="xyz">
            text 1
            <br>
            text 2
        </td>

        <td class="xyz">
            text 1
        </td>
    </tr>
</table>
"""

soup = BeautifulSoup(dat, 'html.parser')
for td in soup.select("table > tr > td.xyz"):
    print(td.get_text(separator=" ", strip=True))
Run Code Online (Sandbox Code Playgroud)

印刷:

text 1 text 2
text 1
Run Code Online (Sandbox Code Playgroud)