'Having trouble extracting inner text of <td> tags in beautifulsoup
I am using bs4 to scrape a website with a list of years.
years = soup.find_all('td', class_='EndCellSpacer')
which returns an array of matching tags:
[<td class="EndCellSpacer">
2014
</td>, <td class="EndCellSpacer">
2015
</td>, <td class="EndCellSpacer">
2016
</td>, <td class="EndCellSpacer">
2017
</td>, <td class="EndCellSpacer">
2018
</td>, <td class="EndCellSpacer">
2019
</td>, <td class="EndCellSpacer">
2020
</td>, <td class="EndCellSpacer">
2021
</td>]
I want the array to only return the years without the <td> tags. I have tried to use
years = soup.find_all('td', class_='EndCellSpacer').text.strip()
but I am getting this error message:
"ResultSet object has no attribute 'text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?"
If I call find(), it only returns the year from the first <td> tag, and I need all of them.
This might have something to do with the values being in an array but I can't seem to figure it out. I would greatly appreciate the help, this is my first time working in Python :/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
