我已经用下面的格式编写了创建输出的漂亮代码
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>
看起来所有的标题都带有标签
<strong>
。所以,您可以做的是创建一个带有该标记的文本列表,然后转换为一个使用pandas写入文件的表。你也可以使用writer,但我喜欢使用pandas,因为你可以根据需要操纵表格输出:
相关问题 更多 >
编程相关推荐