使用Python从rotowire抓取MLB每日阵容
我正在尝试从这个网站抓取MLB每日阵容的信息:https://www.rotowire.com/baseball/daily-lineups.php
我打算使用Python,结合requests、BeautifulSoup和pandas来实现。
我的最终目标是得到两个pandas数据框。
第一个是关于首发投手的数据框:
日期 | 比赛时间 | 投手名字 | 球队 | 投球手型 |
---|---|---|---|---|
2024-03-29 | 下午1:40(东部时间) | 斯宾塞·斯特赖德 | 亚特兰大勇士 | 右投 |
2024-03-29 | 下午1:40(东部时间) | 扎克·韦勒 | 费城费城人 | 右投 |
第二个是关于首发击球手的数据框:
日期 | 比赛时间 | 击球手名字 | 球队 | 位置 | 击球顺序 | 击球手型 |
---|---|---|---|---|---|---|
2024-03-29 | 下午1:40(东部时间) | 罗纳德·阿库尼亚 | 亚特兰大勇士 | 右外野 | 1 | 右打 |
2024-03-29 | 下午1:40(东部时间) | 奥兹·阿尔比斯 | 亚特兰大勇士 | 二垒手 | 2 | 双打 |
2024-03-29 | 下午1:40(东部时间) | 奥斯丁·莱利 | 亚特兰大勇士 | 三垒手 | 3 | 右打 |
2024-03-29 | 下午1:40(东部时间) | 凯尔·施瓦伯 | 费城费城人 | 指定打击 | 1 | 左打 |
2024-03-29 | 下午1:40(东部时间) | 特雷·特纳 | 费城费城人 | 游击手 | 2 | 右打 |
2024-03-29 | 下午1:40(东部时间) | 布莱斯·哈ーパー | 费城费城人 | 一垒手 | 3 | 左打 |
这些数据是针对某一天所有比赛的。
我尝试过调整这个答案以满足我的需求,但似乎没有成功:使用BeautifulSoup抓取网页数据
任何帮助或指导都非常感谢。
这是我尝试调整的链接中的代码,但我似乎没有进展:
import pandas as pd
import requests
from bs4 import BeautifulSoup
url = "https://www.rotowire.com/baseball/daily-lineups.php"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
weather = []
for tag in soup.select(".lineup__bottom"):
header = tag.find_previous(class_="lineup__teams").get_text(
strip=True, separator=" vs "
)
rain = tag.select_one(".lineup__weather-text > b")
forecast_info = rain.next_sibling.split()
temp = forecast_info[0]
wind = forecast_info[2]
weather.append(
{"Header": header, "Rain": rain.text.split()[0], "Temp": temp, "Wind": wind}
)
df = pd.DataFrame(weather)
print(df)
我想要的信息似乎包含在 lineup__main
中,而不是在 lineup__bottom
中。
1 个回答
2
你需要逐个查看这些框,然后选择你想要的所有特征。
import pandas as pd
import requests
from bs4 import BeautifulSoup
url = "https://www.rotowire.com/baseball/daily-lineups.php"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
data_pitiching = []
data_batter = []
team_type = ''
for e in soup.select('.lineup__box ul li'):
if team_type != e.parent.get('class')[-1]:
order_count = 1
team_type = e.parent.get('class')[-1]
if e.get('class') and 'lineup__player-highlight' in e.get('class'):
data_pitiching.append({
'date': e.find_previous('main').get('data-gamedate'),
'game_time': e.find_previous('div', attrs={'class':'lineup__time'}).get_text(strip=True),
'pitcher_name':e.a.get_text(strip=True),
'team':e.find_previous('div', attrs={'class':team_type}).next.strip(),
'lineup_throws':e.span.get_text(strip=True)
})
elif e.get('class') and 'lineup__player' in e.get('class'):
data_batter.append({
'date': e.find_previous('main').get('data-gamedate'),
'game_time': e.find_previous('div', attrs={'class':'lineup__time'}).get_text(strip=True),
'pitcher_name':e.a.get_text(strip=True),
'team':e.find_previous('div', attrs={'class':team_type}).next.strip(),
'pos': e.div.get_text(strip=True),
'batting_order':order_count,
'lineup_bats':e.span.get_text(strip=True)
})
order_count+=1
df_pitching = pd.DataFrame(data_pitiching)
df_batter = pd.DataFrame(data_batter)
日期 | 比赛时间 | 投手名字 | 球队 | 投球手型 | |
---|---|---|---|---|---|
0 | 2024-03-29 | 下午1:40(东部时间) | Freddy Peralta | 酿酒人 | 右投 |
1 | 2024-03-29 | 下午1:40(东部时间) | Jose Quintana | 大都会 | 左投 |
.. | |||||
19 | 2024-03-29 | 晚上10:10(东部时间) | Bobby Miller | 道奇 | 右投 |
日期 | 比赛时间 | 打击者名字 | 球队 | 位置 | 打击顺序 | 打击手型 | |
---|---|---|---|---|---|---|---|
0 | 2024-03-29 | 下午1:40(东部时间) | J. Chourio | 酿酒人 | 右外野 | 1 | 右打 |
1 | 2024-03-29 | 下午1:40(东部时间) | W. Contreras | 酿酒人 | 捕手 | 2 | 右打 |
... | |||||||
178 | 2024-03-29 | 晚上10:10(东部时间) | E. Hernandez | 道奇 | 中外野 | 8 | 右打 |
179 | 2024-03-29 | 晚上10:10(东部时间) | Gavin Lux | 道奇 | 二垒手 | 9 | 左打 |