Веб-скрапинг Python и очистка данныхPython

Программы на Python
Anonymous
 Веб-скрапинг Python и очистка данных

Сообщение Anonymous »

Я пытаюсь получить расписание/результаты с https://www.baseball-reference.com/leag ... dule.shtml и сохранить их в файле csv, который можно импортировать на диск Google. По какой-то причине мой parsed_data.csv отображает только «Дата, Команда гостей, Счет на выезде, Команда хозяев, Счет хозяев», а не фактические необходимые данные.

Код: Выделить всё

import requests
from bs4 import BeautifulSoup
import pandas as pd
import re
import csv

# First script: Fetch the HTML from baseball-reference.com and save it to a file
url = "https://www.baseball-reference.com/leagues/majors/2023-schedule.shtml"
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
with open("2023 MLB Schedule _ Baseball-Reference.com.html", "w") as file:
file.write(soup.prettify())
else:
print(f"Failed to fetch URL with status code {response.status_code}")

# Second script: Load the HTML file, parse it, and save the extracted data to a CSV file
with open('2023 MLB Schedule _ Baseball-Reference.com.html', 'r') as html_file:
html_content = html_file.read()
soup = BeautifulSoup(html_content, 'html.parser')
div = soup.find('div', {'id': 'div_655919807'})
if div:
div_text = div.get_text(separator='\n')
df = pd.DataFrame(div_text.split('\n'), columns=['Text'])
df.to_csv('extracted_data.csv', index=False)
print('Data saved to data.csv')
else:
print('Div not found')

# Third script: Load the CSV file, parse the data, and save it to a new CSV file
df = pd.read_csv('extracted_data.csv')
parsed_data = pd.DataFrame(columns=['Date', 'Away Team', 'Away Score', 'Home Team', 'Home Score'])
for index, row in df.iterrows():
soup = BeautifulSoup(str(row['Text']), 'html.parser')
date_text = soup.find(string=re.compile(r'\w+, [A-Za-z]+ \d+, \d{4}'))
if date_text is None:
continue
date = date_text.strip()
games = soup.find_all(string=re.compile(r'Boxscore'))
for game in games:
away_team = game.find_previous(string=re.compile(r'\w')).strip()
away_score = game.find_previous(string=away_team).find_previous(string=re.compile(r'\d')).strip()
home_team = game.find_previous(string=away_score).find_previous(string=re.compile(r'\w')).strip()
home_score = game.find_previous(string=home_team).find_previous(string=re.compile(r'\d')).strip()
parsed_data = parsed_data.append({'Date': date, 'Away Team': away_team, 'Away Score': away_score, 'Home Team': home_team, 'Home Score': home_score}, ignore_index=True)
parsed_data.to_csv('parsed_data.csv', index=False)
print('Parsed data saved to parsed_data.csv')

# Fourth script: Fetch data from pine-sports.com and save it to a CSV file
url = 'https://www.pine-sports.com/stats/props/MLB/'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table', {'id': 'myTable'})
headers = [th.text.strip() for th in table.find_all('th')]
rows = []
for tr in table.find_all('tr'):
row = [td.text.strip() for td in tr.find_all('td')]
if row:
rows.append(row)
with open('mlb_stats.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(headers)
writer.writerows(rows)
parsed_data.csv должен «очистить» и сохранить файл CSV, чтобы он был организован и чист в таблицах Google.
Вот как я хотел бы, чтобы он выглядел, когда я импортирую его в листы.
Изображение


Подробнее здесь: https://stackoverflow.com/questions/765 ... ng-data-up

Вернуться в «Python»