requests.get Missing Schema错误排查:FBref球队URL格式问题
解决requests.get()触发MissingSchema错误的方案
问题根源
错误提示里的URL https//fbref.com/en/squads/18bb7c10/Arsenal-Stats 缺少了HTTPS协议的冒号,正确的URL格式应为 https://xxx。缺少冒号导致requests无法识别URL协议,进而抛出MissingSchema异常。
代码修正
问题出在生成球队URL的拼接代码行,原代码漏掉了https后的冒号:
team_urls = [f"https//fbref.com{I}" for I in links]
将其修改为:
team_urls = [f"https://fbref.com{I}" for I in links]
完整修正后的代码片段
for year in years: data = requests.get(standing_url) soup = BeautifulSoup(data.text) standing_table = soup.select("table.stats_table")[0] links = standing_table.find_all("a") links = [I.get("href") for I in links] links = [I for I in links if "/squads/" in I] # 修正URL拼接,添加https后的冒号 team_urls = [f"https://fbref.com{I}" for I in links] for team_url in team_urls: team_name = team_url.split("/")[-1].replace("-Stats","").replace("-"," ") data = requests.get(team_url) matches = pd.read_html(data.text, match = "Scores & Fixtures")[0] soup = BeautifulSoup(data.text) links = soup.find_all("a") links = [I.get("href") for I in links] links = [I for I in links if I and "all_comp/shooting/" in I] data = requests.get(f"https://fbref.com{links[0]}") shooting = pd.read_html(data.text, match = "Shooting")[0] shooting.columns = shooting.columns.droplevel()
内容的提问来源于stack exchange,提问作者richard
相关产品推荐
相关产品推荐

