【问题标题】:Write data in a column using CSV module in python在 python 中使用 CSV 模块在列中写入数据
【发布时间】:2019-11-23 14:50:28
【问题描述】:

我已经编写了漂亮的汤代码来创建以下格式的输出

Quatermass 2
Ghostbusters
Life of Brian

我现在想将其写入 csv 文件。但是,我熟悉的唯一 csv 写入函数是 write_row。当我使用它时,它只打印我抓取的最后一个“title_content”对象,即 ->Brian 的生活

我的python代码是

from bs4 import BeautifulSoup
import requests
import re
import csv

html = ['table.html']

with open("table.html", "r") as f:
    contents = f.read()

outputfilename = 'row_writer.csv'
print(outputfilename)

outputfile = open(outputfilename, 'w')          #wb = write and binary - indicates file open for writing in binary
writer = csv.writer(outputfile)
writer.writerow(['Title'])

soup = BeautifulSoup(contents, "lxml")
for name in soup.find_all("td", {"width": "41%"}, string=re.compile(r'^(?!Title$)')):
    title_content = ((name).get_text())
    print(title_content)

writer.write_row([title_content])

谁能帮忙将我的整个内容写入 csv 列?

HTML

    <table width='100%' border='0' cellpadding='0' class='blackbg textheadtitle'>
        <tr>
            <td width='41%' align='left'>Title</td>
                <table width='99%' border='0' cellpadding='1' class="normal">
        <tr>
            <td width='41%' align='left'><strong>Quatermass 2</strong></td>

    <table width='100%' border='0' cellpadding='0' class='blackbg textheadtitle'>
        <tr>
            <td width='41%' align='left'>Title</td>
                <table width='99%' border='0' cellpadding='1' class="normal">
        <tr>
            <td width='41%' align='left'><strong>Ghostbusters</strong></td>

    <table width='100%' border='0' cellpadding='0' class='blackbg textheadtitle'>
        <tr>
            <td width='41%' align='left'>Title</td>
                <table width='99%' border='0' cellpadding='1' class="normal">
        <tr>
            <td width='41%' align='left'><strong>Life of Brian</strong></td>

【问题讨论】:

  • 写行的地方需要缩进,否则不在循环块中。缩进在 Python 中很重要。
  • 太好了,谢谢彼得。尽管我的列数据现在打印为 Title、NULL、Quatermass 2、NULL、Ghostbusters、NULL、Life of Brian、NULL。我输出中的每个第二个单元格都是空白的。知道为什么吗?
  • 用熊猫,很可爱

标签: python csv beautifulsoup writer


【解决方案1】:

看起来标题都带有标签&lt;strong&gt;。所以你可以做的是创建一个带有该标签的文本列表,然后转换为使用 pandas 写入文件的表。您也可以使用 writer,但我喜欢使用 pandas,因为您可以根据需要操作表格。

html = ''' <table width='100%' border='0' cellpadding='0' class='blackbg textheadtitle'>
        <tr>
            <td width='41%' align='left'>Title</td>
                <table width='99%' border='0' cellpadding='1' class="normal">
        <tr>
            <td width='41%' align='left'><strong>Quatermass 2</strong></td>

    <table width='100%' border='0' cellpadding='0' class='blackbg textheadtitle'>
        <tr>
            <td width='41%' align='left'>Title</td>
                <table width='99%' border='0' cellpadding='1' class="normal">
        <tr>
            <td width='41%' align='left'><strong>Ghostbusters</strong></td>

    <table width='100%' border='0' cellpadding='0' class='blackbg textheadtitle'>
        <tr>
            <td width='41%' align='left'>Title</td>
                <table width='99%' border='0' cellpadding='1' class="normal">
        <tr>
            <td width='41%' align='left'><strong>Life of Brian</strong></td>'''




import pandas as pd
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')
titles = soup.find_all('strong')

titles_list = [each.text for each in titles ]

df = pd.DataFrame(titles_list, columns=['Title'])

df.to_csv('output.csv', index=False)

输出:

print (df)
           Title
0   Quatermass 2
1   Ghostbusters
2  Life of Brian

【讨论】:

    猜你喜欢
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2018-11-22
    相关资源
    最近更新 更多