我是 Python 的新手,正在帮助一个学校项目。任何帮助深表感谢。谢谢。到 2004 年和 2003 年时出现错误。这是由 result_list 列表引起的。错误是“ValueError:数组必须都是相同的长度”。我怎样才能引入解决这个问题的代码。分数很重要......
import requests
import pandas as pd
from pandas import ExcelWriter
from bs4 import BeautifulSoup
#from openpyxl.writer.excel import ExcelWriter
import openpyxl
#from openpyxl import load_workbook
import csv
year_id = ['2019','2018','2017','2016','2015','2014','2013','2012','2011','2010','2009','2008','2007','2006','2005','2004','2003']
i=0
while i <= len(year_id)-1:
url = 'https://lehighsports.com/sports/mens-soccer/schedule/' + str(year_id[i])
lehigh = requests.get(url).text
soup = BeautifulSoup(lehigh,'lxml')
date_list = []
for date in soup.find_all('div',class_="sidearm-schedule-game-opponent-date"):
date_list.append(date.get_text(strip=True, separator=' '))
name_list = []
for name in soup.find_all('div',class_="sidearm-schedule-game-opponent-name"):
name_list.append(name.get_text(strip=True, separator=' '))
result_list = []
for result in soup.find_all('div',class_="sidearm-schedule-game-result"):
result_list.append(result.get_text(strip=True, separator=' '))
opp_list = []
for opp in soup.find_all('div',class_="sidearm-schedule-game-opponent-text"):
opp_list.append(opp.get_text(strip=True, separator=' '))
conf_list = []
for conf in soup.find_all('div',class_="sidearm-schedule-game-conference-conference"):
conf_list.append(conf.get_text(strip=True))
dict = {'date':date_list,'opponent':name_list,'result':result_list,'list':opp_list,'conference':conf_list}
df = pd.DataFrame(dict)
workbook1 = openpyxl.load_workbook('lehigh.xlsx')
writer = pd.ExcelWriter('lehigh.xlsx', engine='openpyxl')
writer.book = workbook1
df.to_excel(writer, sheet_name=str(year_id[i]),index=False,startrow=0,startcol=0)
writer.save()
writer.close()
i = i+1