Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

I'm having trouble with BeautifulSoup code finding td-class text in the webpage

$
0
0

I can't get the code to find the text found in the race form table, highlighted in the element below. What are the appropriate elements to actually getting that text?

import requestsfrom bs4 import BeautifulSoup# URL of the webpage to scrapeurl = "https://www.racingandsports.com.au/thoroughbred/horse/smokin-rubi/1978955"# Define the user agent headerheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}# Send a GET request to the URL with the user agent headerresponse = requests.get(url, headers=headers)# Parse the HTML content of the pagesoup = BeautifulSoup(response.content, 'html.parser')# Find the table element with class 'table table-condensed table-striped table-hover tbl-race-form'table_element = soup.find('table', class_='table table-condensed table-striped table-hover tbl-race-form')# Check if the table element is foundif table_element:    # Extract text from the table    table_text = table_element.get_text(separator='\n', strip=True)    print(table_text)else:    print("No table with the specified class found on the page.")

Viewing all articles
Browse latest Browse all 23247

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>