Issue
I am trying to extract span tags using beautifulsoup, the code is giving no output
soup = BeautifulSoup(html, "html.parser")
tags = soup("span")
for tag in tags:
print(tag)
tag:<tr><td>Romina</td><td><span class="comments">97</span></td></tr>
please help me!! Thank you
Solution
You need to call find() or find_all() functions with the target tag as the argument, optionally with other constraints (e.g. class name or id) to filter the HTML elements. For more info see docs.
Try this:
from bs4 import BeautifulSoup
html = '''
<table>
<tr><td>Romina</td><td><span class="comments">97</span></td></tr>
</table>
'''
soup = BeautifulSoup(html, "html.parser")
for elt in soup.find_all("span", class_="comments"):
print(elt.text)
Output:
97
If want to store the elements in a list can use list comprehension.
data = [elt.text for elt in soup.find_all("span", class_="comments")]
Answered By - CodeMonkey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.