I am trying to pull HTML data from baseball-reference.com. I thought going to their website, viewing the page source, the html tags would be within the html code itself. However, after further investigation, the set of html tags that I care about are within comment blocks.
Example: https://www.baseball-reference.com/leagues/AL/2021-standard-batting.shtml Find the tag by "Viewing Source Code":
<div class="table_container" id="div_players_standard_batting">
The code I am looking for is below this line. And if you look above this line, you will see the comment block start <!-- and doesn't end until almost the end of the HTML file.
I can pull the HTML comments with the following code, but it comes with a few issues.
- It is in a list and I care only about the one that has the data
- It comes with new line tags
- I am struggling on how to take the players standard batting string code and reparse it as html code to use BeautifulSoup to grab the data I want.
Code:
from bs4 import BeautifulSoup
from bs4 import Comment
import pandas as pd
import os.path
import requests
r = requests.get("https://www.baseball-reference.com/leagues/majors/2021-standard-batting.shtml")
soup = BeautifulSoup(r.content, "html.parser") # try lxml
Data=[x.extract() for x in soup.find_all(string=lambda text: isinstance(text, Comment))]
Data
Current Environment Settings:
dependencies:
- python=3.9.7
- beautifulsoup4=4.11.1
- jupyterlab=3.3.2
- pandas=1.4.2
- pyodbc=4.0.32
The end goal: Be able to have a pandas dataframe that has each player's data from this web page.
EDIT:
ANSWER:
Changes made to get to my goal: Installed the lxml package via Anaconda Prompt into my environment. Used the following line of code to pull my html data into a dataframe (Provided by: HedgeHog - Thank You!)
pd.read_html([x.extract() for x in soup.find_all(string=lambda text: isinstance(text, Comment)) if 'id="div_players_standard_batting"' in x][0])[0]