html表格转换为csv,将HTML表格转换为CSV fi

如何使用Python和BeautifulSoup将这样的表batting gamelogs table转换为CSV文件?在

我想要第一个标题,它说Rk,Gcar,Gtm等等,而不是表中的任何其他标题(每个月的季节)。在

以下是我目前掌握的代码:from bs4 import BeautifulSoup

from urllib2 import urlopen

import csv

def stir_the_soup():

player_links = open('player_links.txt', 'r')

player_ID_nums = open('player_ID_nums.txt', 'r')

id_nums = [x.rstrip('\n') for x in player_ID_nums]

idx = 0

for url in player_links:

print url

soup = BeautifulSoup(urlopen(url), "lxml")

p_type = ""

if url[-12] == 'p':

p_type = "pitching"

elif url[-12] == 'b':

p_type = "batting"

table = soup.find(lambda tag: tag.name=='table' and tag.has_attr('id') and tag['id']== (p_type + "_gamelogs"))

header = [[val.text.encode('utf8') for val in table.find_all('thead')]]

rows = []

for row in table.find_all('tr'):

rows.append([val.text.encode('utf8') for val in row.find_all('th')])

rows.append([val.text.encode('utf8') for val in row.find_all('td')])

with open("%s.csv" % id_nums[idx], 'wb') as f:

writer = csv.writer(f)

writer.writerow(header)

writer.writerows(row for row in rows if row)

idx += 1

player_links.close()

if __name__ == "__main__":

stir_the_soup()

id_nums列表包含每个播放器的所有id号,用作单独CSV文件的名称。在

对于每一行,最左边的单元格是标记,行的其余部分是标记。除了标题,我怎么把它放在一行?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值