Python Forum

Full Version: Accessing a data-phone tag from an href
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working on a small project using Beautiful Soup.
How would I access the phone number of this :

Output:
<a href="javascript:void(0)" class="mlr__item__cta jsMlrMenu" data-phone="709-635-7316">
I tried this, but not working...

co_phone = each_co.find(class_="mlr__item__cta jsMlrMenu" "data-phone")
Did you check the docs?

from bs4 import BeautifulSoup

html = '''<a href="javascript:void(0)" class="mlr__item__cta jsMlrMenu" 
data-phone="709-635-7316">some tetxt</a>'''
soup = BeautifulSoup(html, 'html.parser')
 	
atag = soup.find(class_="mlr__item__cta jsMlrMenu")
print(atag.get('data-phone'))