Ok, I added some output to show what is working, what isn't, and what I'm attempting to accomplish.
This is my code:
def fetch_chosen_stat(selected_url, selected_attr):
if selected_url:
response = requests.get(selected_url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
print(selected_attr)
print(soup)
chosen_stat = soup.find_all(selected_attr)
chosen_stat_hardcoded = soup.find_all(attrs={'class': 'left', 'data-stat': 'opp'})
if chosen_stat:
for stat in chosen_stat:
print(stat.get_text())
else:
print('Data not found.\n')
if chosen_stat_hardcoded:
for stat in chosen_stat_hardcoded:
print(stat.get_text())
else:
print('Hardcoded Data not found.')
else:
print('Failed to retrive the web page.')
Then this is what I am getting as output (abbreviated unncessary sections):
_______________________________________
attrs={'class': 'left', 'data-stat': 'opp'}
<!DOCTYPE html>
<html class="no-js" data-root="/home/pfr/build" data-version="klecko-" lang="en">
<head>
<meta charset="utf-8"/>
<meta content="ie=edge" http-equiv="x-ua-compatible"/>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=2.0" name="viewport">
<link href="
https://cdn.ssref.net/req/202309261" rel="dns-prefetch"/>
<!-- Quantcast Choice. Consent Manager Tag v2.0 (for TCF 2.0) -->
<script async="true" type="text/javascript">
(function() {
var host = window.location.hostname;
var element = document.createElement('script');
var firstScript = document.getElementsByTagName('script')[0];
var url = '
https://cmp.quantcast.com'
.concat('/choice/', 'XwNYEpNeFfhfr', '/', host,
'/choice.js?tag_version=V2');
var uspTries = 0;
var uspTriesLimit = 3;
element.async = true;
element.type = 'text/javascript';
element.src = url;
...
<!-- End Google Analytics -->
<!-- Start of HubSpot Embed Code -->
<script async="" defer="" id="hs-script-loader" src="//
js.hs-scripts.com/20503178.js" type="text/javascript"></script>
<!-- End of HubSpot Embed Code -->
</body>
<!-- SR -->
</html>
Data not found.
Washington Commanders
New York Giants
Dallas Cowboys
San Francisco 49ers
Cincinnati Bengals
Los Angeles Rams
Seattle Seahawks
Baltimore Ravens
Cleveland Browns
Atlanta Falcons
Houston Texans
Los Angeles Rams
Pittsburgh Steelers
Bye Week
San Francisco 49ers
Chicago Bears
Philadelphia Eagles
Seattle Seahawks
_________________________________
What I'm seeing is the function being called correctly and selected_attr is the correct string.
Then the total HTML page is being correctly found by soup, but the find_all(selected_attr) is not finding the data when the hardcoded find_all(attrs={'class': 'left', 'data-stat': 'opp'}) is able to.
I had tried the following with the same results as well:
selected_attr = "attrs={'class': 'left', 'data-stat': 'opp'}"
chosen_stat = soup.find_all(attrs=selected_attr)