在python中使用CSV模块在列中写入数据

2024-09-27 09:35:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用下面的格式编写了创建输出的漂亮代码

Quatermass 2
Ghostbusters
Life of Brian

我现在想将其写入csv文件。然而,我唯一熟悉的csv写入函数是write_row。当我使用它时,它只打印我刮取的最后一个“title\u content”对象,即->;布莱恩的一生

我的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>

Tags: csvtitletablecontentwidthlefttrclass
1条回答
网友
1楼 · 发布于 2024-09-27 09:35:56

看起来所有的标题都带有标签<strong>。所以,您可以做的是创建一个带有该标记的文本列表,然后转换为一个使用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

相关问题 更多 >

    热门问题