Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all links inside a table

My html page has:

...
<table class="t1" ..>
<tr><td> ... <a href="">...</a> ... <a href="">..</a>
</table>

...

I have:

html = BeautifulSoup(page)

links = html.findAll('a', ?????????)

How can I find all links that are inside this table?

like image 960
Blankman Avatar asked Nov 19 '25 00:11

Blankman


2 Answers

Find the table (by class in this case), then find all the links within it.

html = BeautifulSoup(page)
table = html.find('table', 't1')
links = table.findAll('a')
like image 150
Jonny Buchanan Avatar answered Nov 20 '25 14:11

Jonny Buchanan


More efficient than a raw find, use SoupStrainer:

html  = BeautifulSoup(page, parseOnlyThese=SoupStrainer('table', 't1' ) )
links = html.findAll('a')

See also, the Search by Class documentation.

like image 34
Brock Adams Avatar answered Nov 20 '25 14:11

Brock Adams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!