Issue
When I parse the a <tr>/<td>
table in beautiful soup, the first row is always blank. Is there a way to get rid of this easily?
Current code:
soup = BeautifulSoup(driver.page_source, 'html.parser')
for row in soup.select('tr'):
text_columns = [td.a.get_text(strip=True) if td.a else td.get_text(strip=True) for td in row.select('td')]
print(text_columns)
Current output:
[]
['date 1', 'details 1', 'amount 1', '', 'balance 1']
['date 2', 'details 2', 'amount 2', '', 'balance 2']
[etc....]
Solution
As I see soup.select('tr')
returns a list
so you can do something like this: for row in soup.select('tr')[1:]:
starting from second element.
The full code would be:
soup = BeautifulSoup(driver.page_source, 'html.parser')
for row in soup.select('tr')[1:]:
text_columns = [td.a.get_text(strip=True) if td.a else td.get_text(strip=True) for td in row.select('td')]
print(text_columns)
Answered By - Brad Figueroa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.