Raspberry Lemon
Raspberry Lemon

Reputation: 113

How to get child value of div seperately using beautifulsoup

I have a div tag which contains three anchor tags and have url in them. I am able to print those 3 hrefs but they get merged into one value. Is there a way I can get three seperate values.

Div looks like this:

<div class="speaker_social_wrap">
<a href="https://twitter.com/Sigve_telenor" target="_blank">
    <i aria-hidden="true" class="x-icon x-icon-twitter" data-x-icon-b=""></i>
</a>
<a href="https://no.linkedin.com/in/sigvebrekke" target="_blank">
    <i aria-hidden="true" class="x-icon x-icon-linkedin-in" data-x-icon-b=""></i>
</a>
<a href="https://www.facebook.com/sigve.telenor" target="_blank">
<i aria-hidden="true" class="x-icon x-icon-facebook-f" data-x-icon-b=""></i>
</a>

What I have tried so far:

social_media_url = soup.find_all('div', class_ = 'foo')
  for url in social_media_url:
    print(url)

Expected Result:

http://twitter-url
http://linkedin-url
http://facebook-url

My Output

<div><a twitter-url><a linkedin-url><a facebook-url></div>

Upvotes: 0

Views: 70

Answers (2)

Ram
Ram

Reputation: 4779

You can do like this:

from bs4 import BeautifulSoup
import requests

url = 'https://dtw.tmforum.org/speakers/sigve-brekke-2/'
r = requests.get(url)
soup = BeautifulSoup(r.text,'lxml')
a = soup.find('div', class_='speaker_social_wrap').find_all('a')
for i in a:
    print(i['href'])
https://twitter.com/Sigve_telenor
https://no.linkedin.com/in/sigvebrekke
https://www.facebook.com/sigve.telenor

Upvotes: 1

Sirode
Sirode

Reputation: 587

Your selector gives you the div not the urls array. You need something more like:

social_media_div = soup.find_all('div', class_ = 'foo')
social_media_anchors = social_media_div.find_all('a')
for anchor in social_media_anchors:
  print(anchor.get('href'))

Upvotes: 0

Related Questions