Ниже приведен прекрасный скребок для супа Python, который когда-то был успешным в очистке списков команд от MLB.com.Теперь, когда я пытаюсь запустить код, я получаю следующую ошибку:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x97 in position 0: invalid start byte
После прочтения нескольких потоков Stackoverflow, я думаю, что мне нужно изменить строку «с открытым», но я не понимаю, какизменить мой текущий формат кода без необходимости жертвовать писателем CSV и DF в формате MYSQL.Кто-нибудь знает, как настроить код, чтобы исправить эту проблему utf-8?Заранее спасибо!
import requests
import csv
import pandas as pd
from bs4 import BeautifulSoup
from sqlalchemy import create_engine
team_list={'orioles','yankees','redsox','rays','indians','twins','tigers','whitesox','royals','astros','mariners','athletics',
'angels','rangers','phillies','braves','nationals','marlins','mets','cubs','brewers','cardinals','pirates','reds',
'dodgers','dbacks','rockies','giants','padres','bluejays'}
header_added = False
for team in team_list:
page = requests.get('http://m.{}.mlb.com/roster/'.format(team))
soup = BeautifulSoup(page.text, 'html.parser')
soup.find(class_='nav-tabset-container').decompose()
soup.find(class_='column secondary span-5 right').decompose()
roster = soup.find(class_='page page-index')
names = [n.contents[0] for n in roster.find_all('a')]
ids = [n['href'].split('/')[2] for n in roster.find_all('a')]
number = [n.contents[0] for n in roster.find_all('td', index='0')]
handedness = [n.contents[0] for n in roster.find_all('td', index='3')]
height = [n.contents[0] for n in roster.find_all('td', index='4')]
weight = [n.contents[0] for n in roster.find_all('td', index='5')]
DOB = [n.contents[0] for n in roster.find_all('td', index='6')]
team = [soup.find('meta',property='og:site_name')['content']] * len(names)
with open('MLB_Active_Roster.csv', 'a', newline='') as fp:
f = csv.writer(fp)
if not header_added:
f.writerow(['Name', 'ID', 'Number', 'Hand', 'Height', 'Weight', 'DOB', 'Team'])
header_added=True
f.writerows(zip(names, ids, number, handedness, height, weight, DOB, team))
df = pd.read_csv('MLB_Active_Roster.csv')
engine = create_engine("mysql+pymysql://{user}:{pw}@localhost/{db}"
.format(user="user",
pw="password",
db="mlb"))
conn = engine.connect()
df.to_sql(con=engine, name='mlbactiveroster', if_exists='replace')