Home > Software design >  how get elements in group? Python
how get elements in group? Python

Time:02-20

I need to get the href of each element in a list, how can i do this? on bs4

<div >

<a href="link1" target="_blank" rel="rel" >
    <h1>"test"</h1>
</a>
    <a href="link2" target="_blank" rel="rel" >
        <h1>"test2"</h1>
    </a>
<a href="link3" target="_blank" rel="rel" >
    <h1>"test3"</h1>
</a>            </div>

CodePudding user response:

Select the <div> with class group and iterate over its <a> for example with css selectors and list comprehension:

[x['href'] for x in soup.select('div.group a')]

Example

from bs4 import BeautifulSoup

html = '''
<div >
<a href="link1" target="_blank" rel="rel" >
    <h1>"test"</h1>
</a>
    <a href="link2" target="_blank" rel="rel" >
        <h1>"test2"</h1>
    </a>
<a href="link3" target="_blank" rel="rel" >
    <h1>"test3"</h1>
</a>
</div>
'''
soup = BeautifulSoup(html)

[x['href'] for x in soup.select('div.group a')]

Output

['link1', 'link2', 'link3']
  • Related