gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
beautifulsoup find script id
- User
beautifulsoup find script id
- Assistant
To find a
<script>tag with a specificidattribute using BeautifulSoup in Python, you can use thefindorfind_allmethod. Here’s a basic example demonstrating how to do this:from bs4 import BeautifulSoup # Sample HTML content html_content = """ <html> <head> <script id="target-script" type="text/javascript"> console.log("This is the script you are looking for!"); </script> <script id="another-script"> console.log("Another script"); </script> </head> <body> <h1>Example Page</h1> </body> </html> """ # Create a BeautifulSoup object soup = BeautifulSoup(html_content, 'html.parser') # Find the <script> tag with the specific id 'target-script' script_tag = soup.find('script', id='target-script') # Check if the script tag was found and print its content if script_tag: print("Found script:") print(script_tag) else: print("Script with specified id not found.")