我正在尝试从这里抓取 MLB 每日阵容信息:https://www.rotowire.com/baseball/daily-lineups.php
我正在尝试将 python 与请求、BeautifulSoup 和 pandas 一起使用。
我的最终目标是最终得到两个 pandas 数据框。
首先是起始投球数据帧:
日期 | 比赛时间 | 投手名称 | 团队 | 阵容_投掷 |
---|---|---|---|---|
2024-03-29 | 下午 1:40 美国东部时间 | 斯宾塞·斯泰德 | ATL | R |
2024-03-29 | 下午 1:40 美国东部时间 | 扎克惠勒 | PHI | R |
第二个是起始击球手数据框:
日期 | 比赛时间 | 击球手名称 | 团队 | 位置 | 击球顺序 | 阵容_蝙蝠 |
---|---|---|---|---|---|---|
2024-03-29 | 下午 1:40 美国东部时间 | 罗纳德·阿库纳 | ATL | 射频 | 1 | R |
2024-03-29 | 下午 1:40 美国东部时间 | 奥齐·阿尔比斯 | ATL | 2B | 2 | S |
2024-03-29 | 下午 1:40 美国东部时间 | 奥斯汀·莱利 | ATL | 3B | 3 | R |
2024-03-29 | 下午 1:40 美国东部时间 | 凯尔施瓦伯 | PHI | DH | 1 | L |
2024-03-29 | 下午 1:40 美国东部时间 | 特雷·特纳 | PHI | SS | 2 | R |
2024-03-29 | 下午 1:40 美国东部时间 | 布莱斯哈珀 | PHI | 1B | 3 | L |
这适用于某一天的所有游戏。
我已经尝试根据我的需求调整这个答案,但似乎无法让它正常工作:Scraping Web data using 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
中。
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 美国东部时间 | 弗雷迪·佩拉尔塔 | 酿酒师 | R |
1 | 2024-03-29 | 下午 1:40 美国东部时间 | 何塞·金塔纳 | 大都会 | L |
.. | |||||
19 | 2024-03-29 | 美国东部时间晚上 10:10 | 鲍比·米勒 | 道奇队 | R |
日期 | 比赛时间 | 投手名称 | 团队 | 位置 | 击球顺序 | 阵容_蝙蝠 | |
---|---|---|---|---|---|---|---|
0 | 2024-03-29 | 下午 1:40 美国东部时间 | J。乔里奥 | 酿酒师 | 射频 | 1 | R |
1 | 2024-03-29 | 下午 1:40 美国东部时间 | W。孔特雷拉斯 | 酿酒师 | C | 2 | R |
... | |||||||
178 | 2024-03-29 | 美国东部时间晚上 10:10 | E。埃尔南德斯 | 道奇队 | CF | 8 | R |
179 | 2024-03-29 | 美国东部时间晚上 10:10 | 加文·勒克斯 | 道奇队 | 2B | 9 | L |