我知道这是一个简单的问题,但我无法找出问题所在 ,当我将我的漂亮汤输出到CSV时,我得到的输出是8倍,这也是HTML查找的结果数-我在哪里将输出乘以结果数? pack3_value的输出仅是我需要的数据
pack3_rows = []
for row in pack_rows3:
cells = row.findChildren('td')
pack3_row = [] # - for output
for cell in cells:
pack3_value = cell.string
pack3_row.append(cell.text)
pack3_rows.append(pack3_row)
print (pack3_value)
with open('output.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(pack3_rows)
答案 0 :(得分:0)
每行只能追加到pack3_rows
一次,而不是每个单元格追加一次。
pack3_rows = []
for row in pack_rows3:
cells = row.findChildren('td')
pack3_row = [] # - for output
for cell in cells:
pack3_value = cell.string
pack3_row.append(cell.text)
print (pack3_value)
pack3_rows.append(pack3_row)
如果print
仅用于调试,而不是实际应用程序所需,则可以使用列表推导。
pack3_rows = []
for row in pack_rows3:
cells = row.findChildren('td')
pack3_rows.append([cell.text for cell in cells])