gpt4 book ai didi

python - 在Python CSV Writer循环中一次写入标题

转载 作者:行者123 更新时间:2023-11-28 21:36:15 25 4
gpt4 key购买 nike

下面是一个scraper,它循环浏览两个网站,收集一个团队的花名册信息,将信息放入一个数组中,并将数组导出到CSV文件中。一切正常,但唯一的问题是每次scraper移动到第二个网站时,writerow头都会在csv文件中重复出现。当scraper在多个网站上循环时,是否可以调整代码的CSV部分使标题只出现一次?提前谢谢!

import requests
import csv
from bs4 import BeautifulSoup

team_list={'yankees','redsox'}

for team in team_list:
page = requests.get('http://m.{}.mlb.com/roster/'.format(team))
soup = BeautifulSoup(page.text, 'html.parser')

soup.find(class_='nav-tabset-container').decompose()
soup.find(class_='column secondary span-5 right').decompose()

roster = soup.find(class_='layout layout-roster')
names = [n.contents[0] for n in roster.find_all('a')]
ids = [n['href'].split('/')[2] for n in roster.find_all('a')]
number = [n.contents[0] for n in roster.find_all('td', index='0')]
handedness = [n.contents[0] for n in roster.find_all('td', index='3')]
height = [n.contents[0] for n in roster.find_all('td', index='4')]
weight = [n.contents[0] for n in roster.find_all('td', index='5')]
DOB = [n.contents[0] for n in roster.find_all('td', index='6')]
team = [soup.find('meta',property='og:site_name')['content']] * len(names)

with open('MLB_Active_Roster.csv', 'a', newline='') as fp:
f = csv.writer(fp)
f.writerow(['Name','ID','Number','Hand','Height','Weight','DOB','Team'])
f.writerows(zip(names, ids, number, handedness, height, weight, DOB, team))

最佳答案

使用变量检查是否添加了头可能会有帮助。如果添加页眉,则不会再添加第二次

header_added = False
for team in team_list:
do_some stuff

with open('MLB_Active_Roster.csv', 'a', newline='') as fp:
f = csv.writer(fp)
if not header_added:
f.writerow(['Name','ID','Number','Hand','Height','Weight','DOB','Team'])
header_added = True
f.writerows(zip(names, ids, number, handedness, height, weight, DOB, team))

关于python - 在Python CSV Writer循环中一次写入标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51352928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com