我正在尝试通过使用 Python 3 进行网络抓取,将本网站的表格转换为 .csv 文件: 2011-2012 NBA National Schedule
该表的开头如下:
Revised Schedule Original Schedule
Date Time Game Net Time Game Net
Sun., 12/25/11 12 PM BOS (1) at NY (1) TNT 12 PM BOS (7) at NY (7) ESPN
Sun., 12/25/11 2:30 PM MIA (1) at DAL (1) ABC 2:30 PM MIA (8) at DAL (5) ABC
Sun., 12/25/11 5 PM CHI (1) at LAL (1) ABC 5 PM CHI (6) at LAL (9) ABC
Sun., 12/25/11 8 PM ORL (1) at OKC (1) ESPN no game no game no game
Sun., 12/25/11 10:30 PM LAC (1) at GS (1) ESPN no game no game no game
Tue., 12/27/11 8 PM BOS (2) at MIA (2) TNT no game no game no game
Tue., 12/27/11 10:30 PM UTA (1) at LAL (2) TNT no game no game no game
我只对修订后的时间表感兴趣,即前 4 列。我想要的 .csv 文件中的输出如下所示:
我正在使用这些包:
import re
import requests
import pandas as pd
import numpy as np
from bs4 import BeautifulSoup
from itertools import groupby
这是我为匹配我想要的格式所做的代码:
df = pd.read_html("https://www.sportsmediawatch.com/2011/12/revised-2011-12-nba-national-tv-schedule/", header=0)[0]
revisedCols = ['Date'] + [ col for col in df.columns if 'Revised' in col ]
df = df[revisedCols]
df.columns = df.iloc[0,:]
df = df.iloc[1:,:].reset_index(drop=True)
# Format Date to m/d/y
df['Date'] = np.where(df.Date.str.startswith(('10/', '11/', '12/')), df.Date + ' 11', df.Date + ' 12')
df['Date']=pd.to_datetime(df['Date'])
df['Date']=df['Date'].dt.strftime('%m/%d/%Y')
# Split the Game column
df[['Away','Home']] = df.Game.str.split('at',expand=True)
# Final dataframe with desired columns
df = df[['Date','Time','Away','Home','Net']]
df.columns = ['Date', 'Time', 'Away', 'Home', 'Network']
print(df)
我注意到在“客场”和“主场”列中每个团队名称旁边都有 (1)、(2) 等。我如何实施抓取程序以删除“客场”和“主场”列中每个团队名称旁边的 (1)、(2) 等?
慕标5832272
Cats萌萌
牛魔王的故事
斯蒂芬大帝
相关分类