将ESPN当前周的NFL行复制到pandas数据帧中,可以通过以下步骤完成:
import pandas as pd
import requests
from bs4 import BeautifulSoup
url = "https://www.espn.com/nfl/schedule/_/week/current"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
table = soup.find("table", class_="schedule has-team-logos align-left")
df = pd.DataFrame(columns=["Date", "Time", "Away Team", "Home Team"])
for row in table.find_all("tr"):
cells = row.find_all("td")
if len(cells) == 4:
date = cells[0].text.strip()
time = cells[1].text.strip()
away_team = cells[2].text.strip()
home_team = cells[3].text.strip()
df = df.append({"Date": date, "Time": time, "Away Team": away_team, "Home Team": home_team}, ignore_index=True)
print(df)
这样就可以将ESPN当前周的NFL行复制到pandas数据帧中了。请注意,这只是一个示例代码,具体的实现可能会因网站结构的变化而有所不同。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云