You can use BeautifulSoup's built-in methods to search for and retrieve specific elements from a tag. Here's an example of how to retrieve the text inside an anchor tag with a specific ID:
from bs4 import BeautifulSoup
html = """
<html>
<body>
<a id="my-link" href="https://www.example.com">Click here</a>
</body>
</html>
"""
soup = BeautifulSoup(html, 'html.parser')
my_link = soup.find('a', {'id': 'my-link'})
link_text = my_link.text
print(link_text)
This code finds the anchor tag with an ID of "my-link", retrieves the text inside the tag ("Click here"), and assigns it to the variable link_text
.
Asked: 2023-03-08 11:00:00 +0000
Seen: 17 times
Last updated: Apr 28 '21