Python Forum
Accessing a data-phone tag from an href - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Accessing a data-phone tag from an href (/thread-33467.html)



Accessing a data-phone tag from an href - KatMac - Apr-27-2021

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")



RE: Accessing a data-phone tag from an href - buran - Apr-27-2021

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'))